谢谢~~~~~~
2 回答
已采纳
田心枫
TA贡献1064条经验 获得超383个赞
package com.may.ninety; import java.io.File; import java.io.IOException; public class WeekDemo1 { public static void main(String[] args) { File dir = new File("/tmp/test/"); WeekDemo1.judeDirExists(dir); File file = new File("/tmp/test/b.txt"); WeekDemo1.judeFileExists(file); } // 判断文件是否存在 public static void judeFileExists(File file) { if (file.exists()) { System.out.println("file exists"); } else { System.out.println("file not exists, create it ..."); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } // 判断文件夹是否存在 public static void judeDirExists(File file) { if (file.exists()) { if (file.isDirectory()) { System.out.println("dir exists"); } else { System.out.println("the same name file exists, can not create dir"); } } else { System.out.println("dir not exists, create it ..."); file.mkdir(); } } }
yanrun
TA贡献317条经验 获得超240个赞
public static void main(String[] args) { System.out.println(isFileExists("D:/repo")); } private static boolean isFileExists(String path) { File file = new File(path); return file.exists(); }
添加回答
举报
0/150
提交
取消