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);
allSize += file.length(); } else { System.out.println(book); }
} } } } } }
JAVA
|