java文件重命名,java文件重命名怎麼弄

本文目錄一覽:

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替換,當然也可以使用正則表達式來替換了

   

  }

 }

}

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;

}

}

}

原文鏈接:網頁鏈接

如有幫助請採納(不懂請提問),可以看我主頁,歡迎來交流學習;

IDEAjava文件重新命名

1.右鍵model 然後選擇refactor–rename—選擇rename module–ok

2.file– project structure–修改name為新model名

3.修改pom文件中舊項目名替換為新項目名

4.修改文件名,刷新項目OK

java中對文件怎麼重命名

file

f

=

new

file(“d:/aaa.txt”);//想命名的原文件

f.renameto(new

file(“d:/bbb.txt”));將原文件更改為bbb.txt,其中路徑是必要的

注意

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/276690.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-19 13:20
下一篇 2024-12-19 13:20

相關推薦

發表回復

登錄後才能評論