JAVA 遍曆檔案夾下檔案並變更檔名稱

來源:互聯網
上載者:User

標籤:

  周末因為一些原因,需要批量更改一些檔案的名稱,使其隨機,就隨手寫了點代碼。

增加一個隨機字母:

public static void changeName(String path){File file = new File(path);File[] files = file.listFiles();for (int i = 0; i < files.length; i++) {if(files[i].isDirectory()){changeName(files[i].toString());}else{String name = files[i].getAbsolutePath();files[i].renameTo(new File("G:\\newSong\\"+getChar()+files[i].getName()));}}}public static String getChar(){String[] datas = {"a","b","c","d","e","f","g","h","k","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};Random r = new Random();return datas[r.nextInt(datas.length-1)];}public static void main(String[] args) {changeName("G:\\song");}

  刪除掉隨機字母

public static void changeName(String path){File file = new File(path);File[] files = file.listFiles();for (int i = 0; i < files.length; i++) {if(files[i].isDirectory()){changeName(files[i].toString());}else{String name = files[i].getAbsolutePath();//newSong\String nameStr = files[i].getName().substring(1,files[i].getName().length());files[i].renameTo(new File("G:\\song\\"+nameStr));}}}public static String getChar(){String[] datas = {"a","b","c","d","e","f","g","h","k","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};Random r = new Random();return datas[r.nextInt(datas.length-1)];}public static void main(String[] args) {changeName("G:\\newSong");}

  

JAVA 遍曆檔案夾下檔案並變更檔名稱

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.