获取文件名(不包括后缀)
originalFileName.substring(0, originalFileName.lastIndexOf("."))
文件重命名
public void renameFile(String file, String toFile) {
File toBeRenamed = new File(file);
//检查要重命名的文件是否存在,是否是文件
if (!toBeRenamed.exists() || toBeRenamed.isDirectory()) {
System.out.println("File does not exist: " + file);
return;
}
File newFile = new File(toFile);
//修改文件名
if (toBeRenamed.renameTo(newFile)) {
System.out.println("File has been renamed.");
} else {
System.out.println("Error renmaing file");
}
}renameFile("C:\\temp\\file1.txt", "C:\\temp\\file2.txt");