檔案批量改名初級代碼,批量改名

來源:互聯網
上載者:User

檔案批量改名初級代碼,批量改名

建立了一個用於批量修改檔案名稱的工具類,可以用於在網上下載的電視劇如果命名有網站地址的不需要的命名,可以批量修改成自己想要的名字

例如在網上下載了甄嬛傳72集

結果命名全為"http://isoivjsvlkfdjorwiu pijklsjvgoij ;lkjioju第一集"(亂碼胡亂打的)

      "http://isoivjsvlkfdjorwiu pijklsjvgoij ;lkjioju第二集"等等;

可以批量修改成 "甄嬛傳第一集""甄嬛傳第二集"等

 1 package gaiming; 2  3 import java.io.File; 4 import java.util.Scanner; 5  6 public class GaiMing { 7     public void gai(){ 8         //提醒輸入改名檔案目錄的路徑 9         System.out.println("請輸入路徑:");10         //掃描器11         Scanner input = new Scanner(System.in);12         //擷取目錄路徑13         File file = new File(input.nextLine());14         //擷取目錄下的檔案路徑15         File[] file1 = file.listFiles();16         //提醒輸入要修改的檔案名稱欄位17         System.out.println("請輸入需要改名內容所含的相同字元:");18         String s1 = input.nextLine();19         //提醒輸入要修改成的新的檔案名稱的欄位20         System.out.println("請輸入要改的格式");21         String s2 = input.nextLine();22         //迴圈改名23         for (int i = 0; i < file1.length; i++) {24             //將檔案路徑轉化為字串類型25             String str = file1[i].toString();26             //判斷字串是否含有要修改的欄位27             if(str.contains(s1)){28                 //建立一個臨時的字串來儲存替換後的字串類型路徑29                 String sLS = str.replace(s1, s2);30                 //將修改後的字串類型路徑改為抽象路徑31                 File fileLS = new File(sLS);32                 //將檔案改為抽象路徑所提供的檔案名稱33                 file1[i].renameTo(fileLS);34             }35         }36     }37 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.