修改单个文件
public static void main(String[] args) {
String fileName = "work.doc";
String filePath = "C:/Users/Administrator/Desktop/92个诊疗方案及临床路径/";
File file1 = new File(filePath + fileName );
file1.renameTo(new File(filePath+"new."+ file1.getName().substring(file1.getName().lastIndexOf(".")+1)));
}批量修改
public static void main(String[] args) {
String filePath = "C:/Users/Administrator/Desktop/92个诊疗方案及临床路径/";
File file = new File(filePath);
File[] files = file.listFiles();
int i = 1;
for (File file2 : files) {
if(file2.isFile()){
file2.renameTo(new File(filePath + "NewName" + i + "." + file2.getName().substring(file2.getName().lastIndexOf(".")+1)));
}
i++;
}
}
本文提供了一个使用Java进行文件重命名的示例代码,包括单个文件和批量文件的重命名方法。通过简单的代码片段展示了如何更改文件名及其扩展名。

被折叠的 条评论
为什么被折叠?



