我们能否将一个名为test.txt
的文件重命名为test1.txt
?
如果test1.txt
已存在,它会被重命名吗?
如何将其重命名为已经存在的test1.txt
文件,以便将test.txt的新内容添加到其中以供以后使用?
我们能否将一个名为test.txt
的文件重命名为test1.txt
?
如果test1.txt
已存在,它会被重命名吗?
如何将其重命名为已经存在的test1.txt
文件,以便将test.txt的新内容添加到其中以供以后使用?
从http://exampledepot.8waytrips.com/egs/java.io/RenameFile.html复制
// File (or directory) with old name
File file = new File("oldname");
// File (or directory) with new name
File file2 = new File("newname");
if (file2.exists())
throw new java.io.IOException("file exists");
// Rename file (or directory)
boolean success = file.renameTo(file2);
if (!success) {
// File was not successfully renamed
}
要追加到新文件:
java.io.FileWriter out= new java.io.FileWriter(file2, true /*append=yes*/);
简而言之:
Files.move(source, source.resolveSibling("newname"));
更多细节:
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
以下内容直接从http://docs.oracle.com/javase/7/docs/api/index.html复制而来:
假设我们想将文件重命名为“newname”,并保持文件在相同的目录中:Path source = Paths.get("path/here");
Files.move(source, source.resolveSibling("newname"));
或者,假设我们想要将一个文件移动到一个新目录,保持相同的文件名,并替换目录中任何现有的同名文件:
Path source = Paths.get("from/path");
Path newdir = Paths.get("to/path");
Files.move(source, newdir.resolve(source.getFileName()), StandardCopyOption.REPLACE_EXISTING);
Path source = file.getAbsolutePath();
这里的 file
是需要重命名的文件。 - yurinFiles.move
支持的选项以及它能提供哪些跨平台保证的限制(例如在同一文件系统中重命名时原子替换现有目标文件),请参考以下内容。如何在Java中原子地重命名文件,即使目标文件已经存在? - Peter Cordes对于Java 1.6及以下版本,我认为最安全和最干净的API是Guava的Files.move。
示例:
File newFile = new File(oldFile.getParent(), "new-file-name.txt");
Files.move(oldFile.toPath(), newFile.toPath());
第一行确保新文件的位置与旧文件的父目录相同。
编辑: 我写这篇文章时还没有开始使用Java 7,它引入了一个非常类似的方法。如果您正在使用Java 7+,您应该查看并赞同kr37的答案。
首先,创建一个文件对象来表示目标位置。检查该文件是否存在。如果不存在,则为要移动的文件创建一个新的文件对象。调用要移动的文件的 renameTo 方法,并检查从 renameTo 返回的值以查看调用是否成功。
如果您想将一个文件的内容附加到另一个文件中,则有许多可用的 writer。根据扩展名,它听起来是纯文本,因此我会看一下 FileWriter。
通过将文件移动到新的名称来重命名文件。(FileUtils来自Apache Commons IO库)
String newFilePath = oldFile.getAbsolutePath().replace(oldFile.getName(), "") + newName;
File newFile = new File(newFilePath);
try {
FileUtils.moveFile(oldFile, newFile);
} catch (IOException e) {
e.printStackTrace();
}
这是一个简单的重命名文件的方法:
File oldfile =new File("test.txt");
File newfile =new File("test1.txt");
if(oldfile.renameTo(newfile)){
System.out.println("File renamed");
}else{
System.out.println("Sorry! the file can't be renamed");
}
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardCopyOption.*;
Path yourFile = Paths.get("path_to_your_file\text.txt");
Files.move(yourFile, yourFile.resolveSibling("text1.txt"));
要替换名为"text1.txt"的现有文件:
Files.move(yourFile, yourFile.resolveSibling("text1.txt"),REPLACE_EXISTING);
试一下
File file=new File("Your File");
boolean renameResult = file.renameTo(new File("New Name"));
// todo: check renameResult
注意:我们应该始终检查renameTo返回值,以确保重命名文件成功,因为它取决于平台(不同的操作系统,不同的文件系统),如果重命名失败,它不会抛出IO异常。
是的,你可以使用 File.renameTo() 方法。但是在将文件重命名为新文件时,请记得要有正确的路径。
import java.util.Arrays;
import java.util.List;
public class FileRenameUtility {
public static void main(String[] a) {
System.out.println("FileRenameUtility");
FileRenameUtility renameUtility = new FileRenameUtility();
renameUtility.fileRename("c:/Temp");
}
private void fileRename(String folder){
File file = new File(folder);
System.out.println("Reading this "+file.toString());
if(file.isDirectory()){
File[] files = file.listFiles();
List<File> filelist = Arrays.asList(files);
filelist.forEach(f->{
if(!f.isDirectory() && f.getName().startsWith("Old")){
System.out.println(f.getAbsolutePath());
String newName = f.getAbsolutePath().replace("Old","New");
boolean isRenamed = f.renameTo(new File(newName));
if(isRenamed)
System.out.println(String.format("Renamed this file %s to %s",f.getName(),newName));
else
System.out.println(String.format("%s file is not renamed to %s",f.getName(),newName));
}
});
}
}
}