java判断目录是否存在

 我来答
冰越10号
推荐于2017-12-15 · TA获得超过361个赞
知道小有建树答主
回答量:123
采纳率:100%
帮助的人:103万
展开全部
String filePath = "f:\test"
File file = new File(filePath);
if(file.isDirectory()){ //必须先判断这是不是一个目录,如果是,再判断它存不存在(具体方法名称, //可能有些忘了,类似)
if(file.exists()){
System.out.print("该目录存在");
}
}
千锋教育
2015-12-07 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
展开全部
用File类的exists()方法:
File file = new File("C:\Windows");
System.out.print(file.exists());
//存在输出true,不存在输出false
java.io.File.exists() 方法测试此抽象路径名定义的文件或目录是否存在。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
小白菜
2015-11-09 · 知道合伙人教育行家
小白菜
知道合伙人教育行家
采纳数:1927 获赞数:39626
从2015到现在在狼鸽网络科技有限公司担任外语讲师助教。

向TA提问 私信TA
展开全部
复制下面那段代码既可以
/**

*
* For files not on the acl.read or acl.write list, applets cannot
*
* 1 - check for the existence of the file
* 2 - check the file type
* 3 - check if the file is a directory
* 4 - check the timestamp when the file was last modified
* 5 - check the file's size
* 6 - create a directory
* 7 - rename the file
* 8 - list the files in this file (as if it were a directory)
* 9 - check read
* 10 - check write
*
* Before:
* Assumes /var/mail/root is a file
* Assumes there isn't a directory named /var/mail/testdir
*
* After:
* The directory /var/mail/testdir may have been created,
* if you ran this test from the appletviewer with /var/mail
* on your acl.write list. If that's the case, then you
* should manually delete the test directory /var/mail/testdir
* before running this test again.
*
* @version JDK 1.0 beta
* @author Marianne Mueller
*/

import java.awt.*;
import java.io.*;
import java.lang.*;
import java.applet.*;

public class fileInfo extends Applet {
String myFile = "/var/mail/root";
File f = new File(myFile);
long n;
int y = 10;
int spacing = 17;

// The paint routine drives the 8 above tests
public void paint(Graphics g) {
try {
if (f.exists())
g.drawString("1. Success on File.exists", 10, y);
}
catch (SecurityException e) {
g.drawString("1. Caught security exception on File.exists", 10, y);
}
y = y + spacing;
try {
if (f.isFile())
g.drawString("2. Success on File.type", 10, y);
}
catch (SecurityException e) {
g.drawString("2. Caught security exception on File.isFile", 10, y);
}
y = y + spacing;
try {
File isd = new File("/var/mail");
if (isd.isDirectory())
g.drawString("3. Success on IsDirectory", 10, y);
}
catch (SecurityException e) {
g.drawString("3. Caught security exception on File.isDirectory", 10, y);
}
y = y + spacing;
try {
n = f.lastModified();
g.drawString("4. Success on timestamp: " + n, 10, y);
}
catch (SecurityException e) {
g.drawString("4. Caught security exception on File.lastModified", 10, y);
}
y = y + spacing;
try {
n = f.length();
g.drawString("5. Success on File.length: " + n, 10, y);
}
catch (SecurityException e) {
g.drawString("5. Caught security exception on File.length", 10, y);
}
y = y + spacing;
try {
File td = new File("/var/mail/testdir");
if (td.mkdir())
g.drawString("6. Success on mkdir", 10, y);
}
catch (SecurityException e) {
g.drawString("6. Caught security exception on File.mkdir", 10, y);
}
y = y + spacing;
try {
File nf = new File("/var/mail/bar");
f.renameTo(nf);
g.drawString("7. Success on File.renameTo " + nf.toString(), 10, y);
nf.renameTo(f);
}
catch (SecurityException e) {
g.drawString("7. Caught security exception on File.renameTo", 10, y);
}
y = y + spacing;
try {
File d = new File("/var/mail");
String files[] = null;
files = d.list();
g.drawString("8. Success on File.list, found " + files.length + " files in " + d.toString(), 10, y);
/* for (int i = 0; i < files.length; i++) {
y = y + spacing;
g.drawString(files[i], 10, y);
}*/
}
catch (SecurityException e) {
g.drawString("8. Caught security exception on File.list", 10, y);
}
y = y + spacing;
try {
if (f.canWrite())
g.drawString("9. Success on File.canWrite()", 10, y);
} catch (SecurityException e) {
g.drawString("9. Caught security exception on File.canWrite", 10, y);
}
y = y + spacing;
try {
if (f.canRead())
g.drawString("10. Success on File.canRead()", 10, y);
} catch (SecurityException e) {
g.drawString("10. Caught security exception on File.canRead", 10, y);
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2014-01-16
展开全部
String path = "d:\test";
File file = new File(path);
if(file.exists()){ ... }

如果你想判断是否是目录,用 file.isDirectory()
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友1b00251
2014-01-16 · TA获得超过287个赞
知道小有建树答主
回答量:209
采纳率:100%
帮助的人:151万
展开全部
File file = new File("C:\Program Files");
System.out.print(file.exists());
//存在输出true,不存在输出false
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(4)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式