package com.hzh.Operation;
import java.io.File;
import java.io.IOException;
/**
- @Description:
- @date: 2021年4月22日下午10:02:31
*/
public class FileOperation {
public static void main(String[] args) {
String filePath = "C:\\Users\\小太阳\\Desktop\\java\\wwwwww\\asdsa\\asdasd\\gfhgf\\werew\\w";
String delPath = "C:\\\\Users\\\\小太阳\\\\Desktop\\\\java\\\\wwwwww";
String fileName = "qqq.yxy";
addFile(filePath, fileName);
deleteFold(new File(delPath));
}
/**
* @Description: 创建文件夹及文件
* @param filePath
* @param fileName
*/
public static void addFile(String filePath, String fileName) {
StringBuilder builder = new StringBuilder();
String pathName = builder.append(filePath).append("\\").append(fileName).toString();
System.out.println(pathName);
File file1 = new File(filePath);
File file2 = new File(pathName);
System.out.println((!file1.exists()) || (!file2.exists()));
if ((!file1.exists()) || (!file2.exists())) {
try {
file1.mkdirs();
file2.createNewFile();
System.out.println("创建成功");
} catch (IOException e) {
System.out.println("创建失败");
}
}
}
/**
* @Description: 删除文件夹
* @param delFile
* @return
*/
public static boolean deleteFold(File delFile) {
if (!delFile.exists()) {
System.out.println("文件夹 " + delFile.getAbsolutePath() + "不存在");
return false;
}
// 文件夹
if (delFile.isDirectory()) {
File[] files = delFile.listFiles();
for (File subFile : files) {
boolean isSuccess = deleteFold(subFile);
if (!isSuccess) {
return isSuccess;
}
}
} else {
boolean isSuccess = delFile.delete();
if (!isSuccess) {
return isSuccess;
}
}
if (delFile.isDirectory()) {
return delFile.delete();
} else {
return true;
}
}
}