1. 代码里包含了先创建文件,再删除文件
package com.api.apidemo.tool.file;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Comparator;
import java.util.Random;
public class BatchDeleteFilePerformance {
public static String filePath = "/home/cathay10/workSpace/log";
static int folderMaxDepth = 5;
static int folderCount = 10;
static int fileCount = 10;
public static void main(String[] args) {
long start = System.currentTimeMillis();
Path path = Paths.get(filePath);
createFolder(path, 0, folderCount);
long end = System.currentTimeMillis();
System.out.println("创建文件耗时:" + (end - start) + "ms");
start = System.currentTimeMillis();
deleteFile_3(filePath);
end = System.currentTimeMillis();
System.out.println("删除文件耗时:" + (end - start) + "ms");
}
public static void deleteFile_1(String path) {
Path dirPath = Paths.get(path);
try {
Files.walk(dirPath)
.sorted(Comparator.reverseOrder())
.forEach(BatchDeleteFilePerformance::deleteDirectoryStream);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void deleteDirectoryStream(Path path) {
try {
Files.delete(path);
} catch (IOException e) {
}
}
public static void deleteFile_2(String path) {
File directory = new File(path);
File[] files = directory.listFiles();
for (File file : files) {
if (file.isFile()) {
file.delete();
} else {
deleteFile_2(file.getAbsolutePath());
file.delete();
}
}
directory.delete();
}
public static void deleteFile_3(String path) {
Path dirPath = Paths.get(path);
try {
Files.walk(dirPath)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void deleteFile_4(String path) {
Path dirPath = Paths.get(path);
try {
Files.walkFileTree(dirPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
}
);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void createFolder(Path path, int folderDepth, int folderCount) {
if (folderDepth != 0) {
path = randomFolderName(path);
}
try {
Files.createDirectories(path);
++folderDepth;
for (int i = 1; i <= folderCount; i++) {
if (folderDepth < folderMaxDepth) {
createFolder(path, folderDepth, folderCount);
}
}
if (folderDepth == folderMaxDepth) {
createFile(path);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void createFile(Path path) {
for (int i = 0; i < fileCount; i++) {
Path filePath = randomFileName(path);
try (FileWriter writer = new FileWriter(filePath.toFile())) {
char[] buffer = new char[1024];
StringBuilder sb = new StringBuilder(buffer.length);
for (int x = 0; x < buffer.length; x++) {
sb.append('X');
}
String content = sb.toString();
long bytesToWrite = 1024;
while (bytesToWrite > 0) {
writer.write(content);
bytesToWrite -= content.getBytes().length;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String randomString(int length) {
String letters = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
String result = "";
Random random = new Random();
for (int i = 0; i < length; i++) {
int index = random.nextInt(letters.length());
result += letters.charAt(index);
}
return result;
}
public static Path randomFileName(Path dirPath) {
Path filePath = dirPath.resolve(randomString(6) + ".txt");
File file = filePath.toFile();
if (file.exists()) {
filePath = randomFileName(dirPath);
}
return filePath;
}
public static Path randomFolderName(Path dirPath) {
Path filePath = dirPath.resolve(randomString(6));
File file = filePath.toFile();
if (file.exists()) {
filePath = randomFileName(dirPath);
}
return filePath;
}
}