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();
}
}
}