導航:首頁 > 文件教程 > java下載文件重命名

java下載文件重命名

發布時間:2024-10-06 21:19:24

java中用renameTo文件重命名後,怎麼getname()沒變化,但是文件名已經改掉了啊

renameTo不會改變f本身的name屬性 你沒有對f進行重新賦值 所以沒改

if(f.renameTo(f1)){
System.out.println("文件重命名為:"+f1.getName())
}

或者
if(f.renameTo(f1)){
f = f1 ;
}
System.out.println("文件重命名為:"+f.getName())

② java實現文件批量上傳是否需要將文件重命名(包括圖片,word文檔,錄音),保存到項目中需要注意哪些

要看復情況:
1. 若上傳文件直接保存成制資料庫中的blob欄位,那就無所謂文件名了;
2. 若上傳文件保存到伺服器的某個文件夾中,那麼為了避免重名,上傳的文件一定要重命名,做法一般是:首先生成一串不會和其他文件相同的名稱,例如序列的值、上傳時間(精確到毫秒)等;其次,將上傳的文件保存到該文件名中;最後,向資料庫中記錄原上傳的文件名、以及生成的文件名。這樣,向用戶顯示的是用戶上傳的名稱,但下載時按資料庫中的記錄按圖索驥即可。

③ java 怎麼給文件重命名

java修改文件名可以直接通過右鍵文件名「Rename」實現。

第一步:找到要修改的文件名位置。

第二步:在文件上右擊,選擇「Refactor」下的「Rename」。

第三步:輸入新文件名後,點擊「確定」即可完成修改操作。

④ java 文件重命名的原理有效率高的辦法嗎

調用操作系統api重名文件就是最效率的辦法。java應該就是採用的這種辦法。

⑤ java怎麼實現文件的重命名

File file=new File("D:\\abc.java");
if(file.exists())
{
file.renameTo(new File("d:\\123.txt"));
}

⑥ java文件操作,使用file.renameTo()方法,為什麼不能將文件重命名呢

File file = new File(filepath + File.separator + filename);
改成這樣就行了,

File file = new File(filepath + File.separatorChar+ filename);

下面是測試的全部代碼

String filepath = "D:";
String filename = "data.txt";
File file = new File(filepath + File.separatorChar+ filename);
System.out.println(file);
String newFileName = file.getAbsolutePath().substring(0, filename.lastIndexOf("."));
boolean flag = file.renameTo(new File(newFileName+ "20070831.bak"));
System.out.println(flag);

⑦ 求Java高手幫忙,在文件重命名和提前最後修改時間時為什麼不對

前面的,都是目錄的時間

import java.io.File;
import java.util.Date;
import java.io.IOException;
public class FileDemo1 {
public static void main(String args[]){
File path=new File(".");
path.mkdirs();
File file=new File(path,"myfile.txt");
if(file.exists()){
file.delete();
}
try {
file.createNewFile();
file=new File(file.getName());
} catch (IOException e2){
e2.printStackTrace();
}
System.out.println("文件的名字是:"+file.getName());
System.out.println("文件myfile.txt的絕對路徑是:"+file.getAbsolutePath());
System.out.println("文件是否可讀: "+(file.canRead()?"可讀":"不可讀"));
System.out.println("文件是否是目錄: "+(file.isDirectory()?"是目錄":"不是目錄"));
System.out.println("文件最後修改時間:"+new Date(file.lastModified()));
if(file.renameTo(new File("myfile.java"))){
System.out.println("文件名修改成功!");
System.out.println("文件的名字是:"+file.getName());
}else{
System.out.println("文件重命名失敗!");
}
Long time=file.lastModified()-200*60*60*1000;
file.setLastModified(time);
System.out.println("文件最後修改時間提前200小時後為:"+new Date(file.lastModified()));
}
}

寫代碼,要合理簡潔

⑧ java中重命名之後輸出的文件名為什麼還是給名前的名字

http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/io/File.html
看java api中File,最後一句:
File 類的實例是不可變的;也就是說,一旦創建,File 對象表示的抽象路徑名將永不改變。

還有你的程序,路徑分隔符應該用//

⑨ 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下載文件重命名相關的資料

熱點內容
微信老版本50安卓下載 瀏覽:269
jslocationhref參數 瀏覽:845
java假設檢驗 瀏覽:98
js多文件下載 瀏覽:467
編程培訓需要什麼學歷 瀏覽:65
hibernate需要資料庫設置外鍵嗎 瀏覽:184
linux隊列實現 瀏覽:706
java常量對象 瀏覽:126
javacdi 瀏覽:946
女人為什麼都喜歡蘋果 瀏覽:254
用文本文檔寫搞笑代碼 瀏覽:858
項目文件整理屬於什麼 瀏覽:977
模具編程怎樣分析用什麼策略 瀏覽:193
java下載文件重命名 瀏覽:388
有什麼app可以看圖紙 瀏覽:504
發動機的數據都在哪裡 瀏覽:236
蘋果5s如何製作鈴聲教程 瀏覽:876
win101703卡頓掉幀 瀏覽:624
myboy模擬器ios版本 瀏覽:208
魔獸世界的游戲路徑在哪個文件夾里 瀏覽:192

友情鏈接