Java移除同名不同后缀的文件

删除我们电脑中文件名称相同,但后缀名不一样的文件。
适用场景:电子书格式有多种,但有可能有重复的,我们只需要保留其中的一个即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* 移除同名不同后缀的文件
*/

public class Main {

static int fileCnt = 0;
static int dupeFileCnt = 0;
static Map<String, String> map = new HashMap<>();

static int pdf = 0;
static int epub = 0;
static int mobi = 0;
static int txt = 0;

static int other = 0;

static long allSize = 0L;

public static void main(String[] args) {
String pathName = "XXX";
Map<String, String> result = showFile(pathName);
deleteDupeFile(result);
System.out.println(pdf);
System.out.println(epub);
System.out.println(mobi);
System.out.println(txt);
System.out.println(other);
System.out.println(fileCnt);
System.out.println(dupeFileCnt);
System.out.println(pdf + epub + mobi + txt + other);
System.out.println(allSize);
}

public static Map<String, String> showFile(String pathName) {
File f1 = new File(pathName);
//选择某个文件夹下所有文件
File[] files = f1.listFiles(); //查询文件夹下的所有文件
for (int i = 0; files != null && i < files.length; i++) {
boolean flag2 = files[i].isDirectory();
if (flag2) {
showFile(files[i].getPath()); //递归调用
} else {
String fullPath = files[i].getPath();//获取此文件的路径
int index = fullPath.lastIndexOf(".");
if (index > 0) {
String bookName = fullPath.substring(0, index);
fileCnt++;
if (map.containsKey(bookName)) {
String books = map.get(bookName);
books = books + "=====" + fullPath;
map.put(bookName, books);
} else {
map.put(bookName, fullPath);
dupeFileCnt++;
}
}
}
}
return map;
}

public static void deleteDupeFile(Map<String, String> fileMap) {
Set<Map.Entry<String, String>> keySet = fileMap.entrySet();
for (Map.Entry<String, String> item : keySet) {
String books = item.getValue();
if (books.contains(item.getKey() + ".pdf")) {
books = books.replace(item.getKey() + ".pdf", "");
pdf++;
} else if (books.contains(item.getKey() + ".epub")) {
books = books.replace(item.getKey() + ".epub", "");
epub++;
} else if (books.contains(item.getKey() + ".mobi")) {
books = books.replace(item.getKey() + ".mobi", "");
mobi++;
} else if (books.contains(item.getKey() + ".txt")) {
books = books.replace(item.getKey() + ".txt", "");
txt++;
} else {
books = ""; //其他格式的暂时先不处理
other++;
}

String[] booksArr = books.split("=====");
if (booksArr.length > 1) {
for (String book : booksArr) {
if (null != book && !"".equals(book)) {
File file = new File(book);
if (book.contains(".file") || book.contains(".epub") || book.contains(".mobi") || book.contains(".azw3") || book.contains(".txt")) {
System.out.println("删除" + book);
// file.delete();
allSize += file.length();
} else {
System.out.println(book);
}

}
}
}
}
}
}

JAVA

Java移除同名不同后缀的文件
https://leehoward.cn/2023/04/09/Java移除同名不同后缀的文件/
作者
lihao
发布于
2023年4月9日
许可协议