使用Java重命名文件 [英] Rename a file using Java
本文介绍了使用Java重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们可以将文件重命名为 test.txt
至 test1.txt
?
Can we rename a file say test.txt
to test1.txt
?
如果 test1.txt
存在,它会重命名吗?
If test1.txt
exists will it rename ?
如何将其重命名为现有的test1.txt文件,以便将test.txt的新内容添加到其中供以后使用?
How do I rename it to the already existing test1.txt file so the new contents of test.txt are added to it for later use?
推荐答案
从 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*/);
这篇关于使用Java重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文