2014-12-04 62 views
0

我想检查一个字符串是否是一个有效的文件模式,我想是:Java的检查,如果字符串是有效的文件

如果string = "/abc/def/apple.xml"然后return TRUE

如果string = "/abc/def/"然后return FALSE

如果string = "/abc/def"然后return FALSE

我的代码:

String filepath = "/abc/def/apple.xml"; 
File f = new File(filepath); 

if(f.isFile() && !f.isDirectory()) 
return true; 
else 
    return false; 

随着我的代码设置字符串/abc/def/apple.xml/abc/def/,也都返回false,真的不知道为什么

+0

我想如果(f.exists()&&!f.isDirectory())也没有工作,要么 – hades 2014-12-04 10:15:59

+0

在哪里文件在什么位置? – Maroun 2014-12-04 10:16:13

+1

你想检查提到的文件是否存在于给定的位置,或者你想检查字符串,看看它是否看起来像文件名? – rajesh 2014-12-04 10:17:05

回答

1

你应该试试这个:

String filePath = "yourPath/file.txt"; 
Path path = Paths.get(filePath); 
System.out.println(Files.exists(path, LinkOption.NOFOLLOW_LINKS)); 
1

如果你是很肯定的文件路径,它包含的.xml比你在做你的任务方式也

String path="abc/bcd/efg.xml"; 
boolean temp=path.endsWith(".xml") ; 
+0

@ prateek谢谢.. – Kandy 2014-12-04 11:25:17

相关问题