java中如何判断多级路径是否存在,不存在则创建
内容摘要
方案一:(带文件名的Path:如:D:
ews201412abc.text)例如:public boolean isexitsPath(String path)throws InterruptedException{
String [] paths=path.split("\");
S
ews201412abc.text)例如:public boolean isexitsPath(String path)throws InterruptedException{
String [] paths=path.split("\");
S
文章正文
方案一:
(带文件名的Path:如:D: ews201412abc.text)
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public boolean isexitsPath(String path)throws InterruptedException{ String [] paths=path.split("\"); StringBuffer fullPath= new StringBuffer(); for (int i = 0; i < paths.length; i++) { fullPath.append(paths[i]).append("\"); File file= new File(fullPath.toString()); if (paths.length-1!=i){ //判断path到文件名时,无须继续创建文件夹! if (!file.exists()){ file. mkdir (); System.out.println( "创建目录为:" +fullPath.toString()); Thread.sleep(1500); } } } File file= new File(fullPath.toString()); //目录全路径 if (!file.exists()) { return true; } else { return false; } } |
注意:带文件名的path,需要判断是否path中已经包含文件名,若包含,则不再创建文件夹。
在线视频教程分享:java在线教程
(不带文件名的Path:如:D: ews201412)
例如:
方案二:
(带文件名的Path:如:D: ews201412abc.text)
(不带文件名的Path:如:D:
ews201412)
注意:带文件名和不带文件名的处理方式的区别就在于循环的长度上。
推荐相关文章教程:java入门程序
代码注释
[!--zhushi--]