java上傳文件重命名
Ⅰ java怎麼修改文件名稱
一般的操作:
Filefile=newFile("word.txt");
Stringfilename=newString("word1.txt");//更改後的文件名
file.renameTo(filename);//更改文件名操作
判斷是否已經更改文件名:
Filefile=newFile("word.txt");
Stringfilename=newString("word1.txt);
if(file.renameTo(filename)){
System.out.println("修改文件名成功");
}else{
System.out.println("修改文件名失敗");
}
file是更改文件名之前的文件,可以為絕對路徑或相對路徑:
絕對路徑可表示為:File file=new File("D:/word.txt");
相對路徑的根文件夾與src文件夾同級,如File file=new File("worddir/word.txt");中的worddir文件夾與src同級
filename是更改文件名之後的文件的文件名(包含後綴)
Ⅱ java中怎麼重命名一個文件
File f = new File("d:/aaa.txt");//想命名的原文件
f.renameTo(new File("d:/bbb.txt"));將原文件更改為bbb.txt,其中路徑是必要的 注意
Ⅲ java中對文件怎麼重命名
file
f
=
new
file("d:/aaa.txt");//想命名的原文件
f.renameto(new
file("d:/bbb.txt"));將原文件更改為bbb.txt,其中路徑是必要的
注意
Ⅳ JAVA 使文件件裡面的所有文件重命名新文件名(求源代碼)
File file = new File("D:\\temp\\A\\B\\");
for(File fl: file.listFiles()){
String flName = fl.getPath().substring(3).replace("\\", "-");
System.out.println(flName);
File newFl = new File(fl.getParent() + File.separator + flName);
System.out.println(newFl.getPath());
fl.renameTo(newFl);
}
Ⅳ 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實現文件批量上傳是否需要將文件重命名(包括圖片,word文檔,錄音),保存到項目中需要注意哪些
要看情況:
1. 若上傳文件直接保存成資料庫中的blob欄位,那就無所謂文件名了;
2. 若上傳文件保存到伺服器的某個文件夾中,那麼為了避免重名,上傳的文件一定要重命名,做法一般是:首先生成一串不會和其他文件相同的名稱,例如序列的值、上傳時間(精確到毫秒)等;其次,將上傳的文件保存到該文件名中;最後,向資料庫中記錄原上傳的文件名、以及生成的文件名。這樣,向用戶顯示的是用戶上傳的名稱,但下載時按資料庫中的記錄按圖索驥即可。