閒來無事,想着自己的硬盤裏大大小小存了一堆的文件,想去根據文件大小或者修改時間對全盤進行掃描排序,好像windows系統上並不支持這樣的功能(只能在單個目錄下對當前目錄中文件進行排序)。那麼咱們就做一個定製化的文件掃描工具,支持如下功能:
- 指定具體目錄,將該目錄下所有文件(包括目錄下嵌套的目錄文件)進行掃描;
- 掃描結果輸出到指定csv中;
- 可選擇根據文件大小排序或者文件修改時間排序;
- 可對目錄下掃描內容做關鍵字過濾功能;
文件輸出後效果如下:
本文屬個人成果,不涉及任何公司機密,故貼出完整實現代碼,拋磚引玉,供大家參考。
import org.apache.commons.io.FileUtils;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class FileScan {
private static final int SORT_BY_SIZE = 1;
private static final int SORT_BY_TIME = 2;
public static void main(String[] args) {
// 讀取文件
File file = FileUtils.getFile("D:\\迅雷下載");
// 遍歷文件下目錄和文件
Map<Long, String> fileMap = buildSortMap();
// 搜索關鍵字
String[] filter = {"txt", "exe", "csv"};
// 把遍歷後的結果進行存儲
scanFiles(file, fileMap, SORT_BY_SIZE, filter);
// 對於存儲結果進行輸出
outputCSV("D:\\111.csv", fileMap);
}
/**
* 構建排序後的map
*
* @return
*/
private static Map<Long, String> buildSortMap() {
return new TreeMap<Long, String>(new Comparator<Long>() {
public int compare(Long o1, Long o2) {
return o2.compareTo(o1); //用正負表示大小值
}
});
}
/**
* 輸出CSV
*
* @param outputPath
* @param fileMap
*/
private static void outputCSV(String outputPath, Map<Long, String> fileMap) {
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputPath), "UTF-8"));) {
String bom = new String(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
String strings = bom + "文件路徑,文件大小,修改日期";
out.write(strings);
out.newLine();
for (Map.Entry<Long, String> entry : fileMap.entrySet()) {
out.write(bom + entry.getValue());
out.newLine();
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 掃描文件
*
* @param file
* @param map
* @return
*/
private static Map<Long, String> scanFiles(File file, Map<Long, String> map, int sortType, String[] filter) {
if (!file.exists()) {
return new HashMap<Long, String>();
}
File[] fileList = file.listFiles();
for (File f : fileList) {
if (f.isDirectory()) {
scanFiles(f, map, sortType, filter);
} else {
try {
if (sortType == SORT_BY_SIZE) {
saveDataBySize(map, f, filter);
} else if (sortType == SORT_BY_TIME) {
saveDataByTime(map, f, filter);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return map;
}
/**
* 根據文件大小排序,存儲
*
* @param map
* @param f
* @throws IOException
*/
private static void saveDataBySize(Map<Long, String> map, File f, String[] filter) throws IOException {
StringJoiner sj = new StringJoiner(",");
buildFileData(map, f, filter, sj);
}
/**
* 根據文件修改時間排序,存儲
*
* @param map
* @param f
* @throws IOException
*/
private static void saveDataByTime(Map<Long, String> map, File f, String[] filter) throws IOException {
StringJoiner sj = new StringJoiner(",");
buildFileData(map, f, filter, sj);
}
/**
* 封裝map信息
*
* @param map
* @param f
* @param filter
* @param sj
* @throws IOException
*/
private static void buildFileData(Map<Long, String> map, File f, String[] filter, StringJoiner sj) throws IOException {
if (filter.length == 0) {
sj.add(f.getCanonicalPath());
sj.add(transformSize(f.length()));
sj.add(getTime(f));
map.put(f.length(), sj.toString());
} else {
for (String fil : filter) {
if (f.getCanonicalPath().toLowerCase(Locale.US).contains(fil)) {
sj.add(f.getCanonicalPath());
sj.add(transformSize(f.length()));
sj.add(getTime(f));
map.put(f.length(), sj.toString());
}
}
}
}
/**
* 獲取
*
* @param file
* @return
*/
private static Long getFileTime(File file) {
return file.lastModified();
}
/**
* 獲取時間
*
* @param file
* @return
*/
private static String getTime(File file) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cal.setTimeInMillis(getFileTime(file));
return formatter.format(cal.getTime());
}
/**
* 文件大小單位轉換
*
* @param size
* @return
*/
private static String transformSize(Long size) {
Long sizeGb = size / (1024 * 1024 * 1024);
Long sizeMb = size / (1024 * 1024);
Long sizeKb = size / (1024);
if (sizeGb > 0) {
return sizeGb + "G";
} else if (sizeMb > 0) {
return sizeMb + "M";
} else if (sizeKb > 0) {
return sizeKb + "K";
} else {
return size + "B";
}
}
}