查看目录(文件)是否存在,不存在则创建。
创建目录
/**
* 创建目录
*
* @param path
*/
public static void CreatFileDir(String path) {
try {
File file = new File(path);
if(file.getParentFile().isDirectory()){//判断上级目录是否是目录
if(!file.exists()){ //如果文件目录不存在
file.mkdirs(); //创建文件目录
}
}else{
throw new Exception("传入目录非标准目录名");
}
} catch (Exception e) {
e.printStackTrace();
}
}
创建文件:
public static void CreatFile(String path) {
try {
File file = new File(path);
if(file.getParentFile().isDirectory()){//判断上级目录是否是目录
if(!file.exists()){ //如果文件不存在
file.createNewFile(); //创建文件
}
}else{
throw new Exception("传入目录非标准文件名");
}
} catch (Exception e) {
e.printStackTrace();
}
}