在java中将文件重命名为另一个文件 [英] Rename file onto another in java

查看:95
本文介绍了在java中将文件重命名为另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件需要重命名为现有文件。这是对现有JAR文件的复制,修改,替换原始操作。我已经完成了前两个步骤,我只需要帮助替换原始位。将新版本的JAR重命名为旧版本的最佳方法是什么。旧的JAR不需要保留,我不希望有一个新的副本,其初始名称仍然存在。

I have a file I need to rename to that of an existing file. This is a copy, modify, replace original operation on an existing JAR file. I've got the first two steps done, I just need help with the replace original bit. What's the best way to rename the new version of the JAR to that of the old. The old JAR doesn't need preserving and I don't want to have a copy of the new with its initial name sticking around.

我已经知道lang和io了,所以如果我错过了一种方法,那就太棒了。

I have commons lang and io already, so if there's a method I've missed, that would be great.

推荐答案

您将需要创建两个 java.io.File 对象:一个用于新文件,一个用于旧文件。

You're going to need to create two java.io.File objects: one for the new file, one for the old file.

让我们称这些 oldFile newFile

oldFile.delete()
newFile.renameTo(oldFile);

编辑:mmyers打败了我。

mmyers beat me to it.

这篇关于在java中将文件重命名为另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆