使用 Java 重命名文件
我们可以将文件 test.txt
重命名为 test1.txt
吗?
如果 test1.txt
存在,它会重命名吗?
如何将其重命名为已经存在的 test1.txt 文件,以便将 test.txt 的新内容添加到其中以供以后使用?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
复制自 http://exampledepot.8waytrips.com/egs/java.io/RenameFile .html
附加到新文件:
Copied from http://exampledepot.8waytrips.com/egs/java.io/RenameFile.html
To append to the new file:
简而言之:
更多详细信息:
以下内容直接从 http://docs 复制.oracle.com/javase/7/docs/api/index.html:
假设我们想要将一个文件重命名为“newname”,并将该文件保留在同一目录中:
或者,假设我们想要移动一个文件到新目录,保留相同的文件名,并替换目录中该名称的任何现有文件:
In short:
More detail:
The following is copied directly from http://docs.oracle.com/javase/7/docs/api/index.html:
Suppose we want to rename a file to "newname", keeping the file in the same directory:
Alternatively, suppose we want to move a file to new directory, keeping the same file name, and replacing any existing file of that name in the directory:
对于 Java 1.6 及更低版本,我相信最安全、最干净的 API 是 Guava 的 文件.move。
示例:
第一行确保新文件的位置是同一目录,即旧文件的父目录。
编辑:
我在开始使用 Java 7 之前写了这篇文章,Java 7 引入了一种非常相似的方法。 因此,如果您使用的是 Java 7+,您应该查看并投票 kr37 的答案。
For Java 1.6 and lower, I believe the safest and cleanest API for this is Guava's Files.move.
Example:
The first line makes sure that the location of the new file is the same directory, i.e. the parent directory of the old file.
EDIT:
I wrote this before I started using Java 7, which introduced a very similar approach. So if you're using Java 7+, you should see and upvote kr37's answer.
您想利用 文件上的 ">renameTo 方法对象。
首先,创建一个 File 对象来表示目标。 检查该文件是否存在。 如果不存在,则为要移动的文件创建一个新的 File 对象。 对要移动的文件调用 renameTo 方法,并检查 renameTo 的返回值是否调用成功。
如果您想将一个文件的内容追加到另一个文件中,可以使用多种编写器。 基于扩展名,听起来像是纯文本,所以我会查看 文件写入器。
You want to utilize the renameTo method on a File object.
First, create a File object to represent the destination. Check to see if that file exists. If it doesn't exist, create a new File object for the file to be moved. call the renameTo method on the file to be moved, and check the returned value from renameTo to see if the call was successful.
If you want to append the contents of one file to another, there are a number of writers available. Based on the extension, it sounds like it's plain text, so I would look at the FileWriter.
通过将文件移至新名称来重命名该文件。 (FileUtils 来自 Apache Commons IO lib)
Renaming the file by moving it to a new name. (FileUtils is from Apache Commons IO lib)
这是重命名文件的简单方法:
This is an easy way to rename a file:
要将现有文件替换为名称“text1.txt”:
To replace an existing file with the name "text1.txt":
试试这个
注意:
我们应该始终检查 renameTo 返回值以确保重命名文件成功,因为它依赖于平台(不同的操作系统,不同的文件系统),并且如果重命名失败,它不会抛出 IO 异常。
Try This
Note :
We should always check the renameTo return value to make sure rename file is successful because it’s platform dependent(different Operating system, different file system) and it doesn’t throw IO exception if rename fails.
是的,您可以使用 File.renameTo()。 但请记住在将其重命名为新文件时要有正确的路径。
}
Yes, you can use File.renameTo(). But remember to have the correct path while renaming it to a new file.
}
如果只是重命名文件,可以使用 File.renameTo()。
如果您想将第二个文件的内容附加到第一个文件,请查看 带有附加构造函数选项的 FileOutputStream 或 FileWriter 也是如此。 您需要读取文件的内容以附加并使用输出流/编写器将其写出。
If it's just renaming the file, you can use File.renameTo().
In the case where you want to append the contents of the second file to the first, take a look at FileOutputStream with the append constructor option or The same thing for FileWriter. You'll need to read the contents of the file to append and write them out using the output stream/writer.
据我所知,重命名文件不会将其内容附加到具有目标名称的现有文件的内容中。
有关在 Java 中重命名文件的信息,请参阅
File
类中的renameTo()
方法的文档。As far as I know, renaming a file will not append its contents to that of an existing file with the target name.
About renaming a file in Java, see the documentation for the
renameTo()
method in classFile
.有效,但仅当您关闭(或自动关闭)所有使用的资源(
InputStream
、FileOutputStream
等)时我认为与file.renameTo
的情况相同> 或FileUtils.moveFile
。works, but only when you close (or autoclose) ALL used resources (
InputStream
,FileOutputStream
etc.) I think the same situation withfile.renameTo
orFileUtils.moveFile
.这是我成功重命名文件夹中的多个文件的代码:
并运行它作为示例:
Here is my code to rename multiple files in a folder successfully:
and run it for an example:
我不喜欢
java.io.File.renameTo(...)
因为有时它不会重命名文件,而你不知道为什么! 它只是返回 true 或 false。 如果失败,它不会抛出异常。另一方面,
java.nio.file.Files.move(...)
更有用,因为它在失败时抛出异常。I do not like
java.io.File.renameTo(...)
because sometimes it does not rename the file and you do not know why! It just returns true or false. It does not throw an exception if it fails.On the other hand,
java.nio.file.Files.move(...)
is more useful as it throws an exception when it fails.file.renameTo(File(file.parent, text))
file.renameTo(File(file.parent, text))
运行代码在这里。
Running code is here.