我们可以重命名一个文件吗test.txt
to test1.txt
?
If test1.txt
存在会重命名吗?
如何将其重命名为已经存在的 test1.txt 文件,以便将 test.txt 的新内容添加到其中以供以后使用?
复制自http://exampledepot.8waytrips.com/egs/java.io/RenameFile.html 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*/);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)