首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 實現文件批量重命名與指定類型文件提取工具完整源碼解析

     編輯:Java 相關知識

專案背景與功能概覽

針對下載視頻教程檔名過長(含機構廣告)、課件資料夾層級過深難以歸檔等痛點,基於 Java Swing 開發桌面工具,核心功能:

  • 功能一:批量重命名 —— 指定目錄下移除/替換檔名關鍵字,支援撤銷操作。
  • 功能二:類型文件提取 —— 遞歸掃描指定路徑,提取特定擴展名文件至目標資料夾,自動處理重名(尾部加序號),支援撤銷刪除。

核心類架構設計

1. 入口與 GUI:TestFrame → ShowGui

// TestFrame.java
public class TestFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new ShowGui().setVisible(true));
    }
}

// ShowGui.java:使用 BoxLayout/FlowLayout 構建雙功能面板
// 6 個 JTextField 輸入路徑/關鍵字/類型,6 個 JButton 觸發操作
// 背景圖片由 BgSet 類透過 LayeredPane 實現

2. 批量重命名核心:RenameFunction & RevokeRename

// RenameFunction.sure(sourceDir, keyword, replacement)
public static void sure(String dir, String keyword, String replacement) {
    File file = new File(dir);
    String[] names = file.list(); // 獲取原始檔名陣列(供撤銷用)
    test1 = names; // 靜態變量備份
    
    File[] files = file.listFiles();
    for (int i = 0; i < names.length; i++) {
        if (names[i].contains(keyword)) {
            names[i] = names[i].replace(keyword, replacement);
        }
    }
    // 實際重命名:遍歷 File 陣列對應修改
    for (File f : files) {
        if (f.isFile()) {
            f.renameTo(new File(dir + "/" + names[i++]));
        }
    }
}

// RevokeRename.revoke(dir) 將 test1 備份的原始檔名恢復

3. 文件提取核心:SearchFileFunction

public static void startCopy(String srcPath, String targetPath, String fileType) {
    xunHuan(srcPath, targetPath, fileType);
}

private static void xunHuan(String current, String target, String type) {
    File file = new File(current);
    if (file.isFile() && file.getAbsolutePath().endsWith(type)) {
        // 目標文件存在則重命名:檔名.擴展名 → 檔名{count}.擴展名
        String dest = target + "/" + file.getName();
        File destFile = new File(dest);
        if (destFile.exists()) {
            String[] parts = dest.split("\\."); // 注意轉義點號
            dest = parts[0] + (count++) + "." + parts[1];
        }
        fileCopyByBufferStreamArray(file.getAbsolutePath(), dest);
    } else if (file.isDirectory()) {
        // 遞歸掃描子目錄
        for (String name : file.list()) {
            xunHuan(file.getAbsolutePath() + "/" + name, target, type);
        }
    }
}

// 緩衝流複製:10 bytes 緩衝區(示例用,生產建議 8KB+)
public static void fileCopyByBufferStreamArray(String src, String dest) { ... }

4. 撤銷提取:RemoveTargetFile

public static void startDelete(String targetPath) {
    deleteFile(new File(targetPath));
}

private static void deleteFile(File file) {
    if (file.isFile()) file.delete();
    else if (file.isDirectory()) {
        for (File f : file.listFiles()) deleteFile(f); // 遞歸刪除,保留目標資料夾本身
    }
}

關鍵技術點總結

模組核心 API注意事項
GUI 佈局FlowLayout, BoxLayout, LayeredPane空佈局需手動 setBounds;背景圖片用 JLabel + LayeredPane 最底層
文件操作File.list(), listFiles(), renameTo()renameTo 跨分區可能失敗;建議改用 Files.move(StandardCopyOption.REPLACE_EXISTING)
流複製BufferedInputStream/OutputStream緩衝區過小(10B)效率極低,實際應用建議 8192 bytes
撤銷機制靜態變量備份原始檔名陣列單例模式下靜態變量共享,多線程或多次操作會串擾,需改為實例變量+會話管理

改進建議

  1. 使用 java.nio.file (Java 7+) 替代舊 IO API,支援符號鏈接、屬性讀取、原子移動。
  2. 引入線程池(ExecutorService)並行處理文件複製,顯示進度條。
  3. 採用 MVC 模式分離 UI 與業務邏輯,便於單元測試。
  4. 增加文件內容校驗(MD5/SHA256)避免同名不同內容文件覆蓋。
  5. 打包為可執行 JAR 或使用 jpackage 生成原生安裝包,替代舊 exe4j 方案。

總結

本工具展示了 Java Swing 開發桌面應用的完整流程:GUI 構建、事件監聽、文件遞歸遍歷、流式複製、狀態備份撤銷。雖然代碼存在硬編碼佈局、緩衝區過小、靜態變量共享等問題,但作為入門級文件處理工具的教學案例極具參考價值。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved