本文目錄一覽:
java文件重命名問題,為什麼文件無法重命名
java修改文件名可以直接通過右鍵文件名「Rename」實現。 第一步:找到要修改的文件名位置。 第二步:在文件上右擊,選擇「Refactor」下的「Rename」。 第三步:輸入新文件名後,點擊「確定」即可完成修改操作。
java如何重命名一個文件
/**
* 修改文件名
* @param oldFilePath 原文件路徑
* @param newFileName 新文件名稱
* @param overriding 判斷標誌(如果存在相同名的文件是否覆蓋)
* @return
*/
public static boolean renameFile(String oldFilePath,String newFileName,boolean overriding){
File oldfile = new File(oldFilePath);
if(!oldfile.exists()){
return false;
}
String newFilepath = oldfile.getParent()+File.separator+newFileName;
File newFile = new File(newFilepath);
if(!newFile.exists()){
return oldfile.renameTo(newFile);
}else{
if(overriding){
newFile.delete();
return oldfile.renameTo(newFile);
}else{
return false;
}
}
}
原文鏈接:網頁鏈接
如有幫助請採納(不懂請提問),可以看我主頁,歡迎來交流學習;
java 文件夾重命名
package com.nokia;
import java.io.File;
/*
* This is class used for rename the whole file under file folder name*/
public class RenameFile {
public static void main(String args[]) {
/*
* you should change the path E://文件夾 to what you have on your own computer!*/
File fl = new File(“E://文件夾”); //這裡寫上發替換的文件夾路徑,注意使用雙斜杠
String[] files = fl.list();
File f = null;
String filename = “”;
for(String file:files){
f = new File(fl,file);//注意,這裡一定要寫成File(fl,file)如果寫成File(file)是行不通的,一定要全路徑
filename = f.getName();
// System.out.println(filename);
/*the string 要替換掉的內容 is the content in your own file string with the name 替換成的內容,
* here you should change the string into what you have.*/
f.renameTo(new File(fl.getAbsolutePath() + “//” + filename.replace(“要替換掉的內容”, “替換成的內容”)));//這裡可以反覆使用replace替換,當然也可以使用正則表達式來替換了
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/270773.html