Main.java
package main;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
public class Main {
String absolutePath = "C:\\Users\\ \\Desktop\\~♪";
ArrayList<String> deleteFileList = new ArrayList<String>(Arrays.asList(
".gitignore",
"Tests.java",
"LogAspct.java"
));
public static void main(String[] args) throws IOException {
new Main();
}
public Main() throws IOException {
findAllFile(absolutePath);
deleteEmptyDirectory(absolutePath);
}
public List<File> findAllFile(String absolutePath) throws IOException {
List<File> fileList = new ArrayList<>();
Stack<File> stack = new Stack<>();
stack.add(new File(absolutePath));
while (!stack.isEmpty()) {
File item = stack.pop();
if (item.isFile()) {
fileList.add(item);
deleteFile(deleteFileList, item);
}
if (item.isDirectory()) {
for (File child : item.listFiles()) {
stack.push(child);
}
}
}
return fileList;
}
public static void deleteEmptyDirectory(String target) {
System.out.println(target);
Stack<File> stack = new Stack<>();
try {
File directory = new File(target);
File[] files = directory.listFiles();
for(File file: files) {
if (file.isDirectory()) {
stack.add(file);
deleteEmptyDirectory(file.getPath());
}
}
while (!stack.isEmpty()) {
File file = stack.pop();
System.out.println("dir削除:" + directory.getAbsolutePath());
ファイル消える~♪ file.delete()
}
} catch (Exception e) {
System.out.println(e);
}
}
private void deleteFile(List<String> dList, File file) throws IOException {
for(String fileName : dList) {
if(file.getAbsolutePath().contains(fileName)) {
return;
} else {
}
}
System.out.println("ファイル削除:" + file.getAbsolutePath());
ファイル消える!! file.delete()
}
}