1、判断文件是否存在,不存在就新建
File file = new File("d:\\hello.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件已创建");
} else {
System.out.println("文件已存在");
}
2、判断文件夹是否存在,不存在就新建
File folder = new File("d:\\hello1\\hello2");
if (!folder.exists() && !folder.isDirectory()) {
folder.mkdirs();
System.out.println("创建文件夹");
} else {
System.out.println("文件夹已存在");
}