File java的檔案重新命名
阿新 • • 發佈:2019-01-31
當新檔名已經存在時可以選擇刪除它
/** *//**檔案重新命名 * @param path 檔案目錄 * @param oldname 原來的檔名 * @param newname 新檔名 */ public void renameFile(String path,String oldname,String newname)...{ if(!oldname.equals(newname)){//新的檔名和以前檔名不同時,才有必要進行重新命名 File oldfile=new File(path+"/"+oldname); File newfile=new File(path+"/"+newname); if(!oldfile.exists()){ return;//重新命名檔案不存在 } if(newfile.exists())//若在該目錄下已經有一個檔案和新檔名相同,則不允許重新命名 System.out.println(newname+"已經存在!"); else{ oldfile.renameTo(newfile); } }else{ System.out.println("新檔名和舊檔名相同..."); } }