Java File 对象的 renameTo 方法删除我的文件而不是重命名它。威斯塔64

发布于 2024-11-11 13:45:56 字数 905 浏览 9 评论 0原文

尝试获取一个 mp3 文件并使用字符串变量重命名它。例如,我有一个古典音乐文件夹 C:/classical,我想要一首名为 vivaldi 的歌曲重命名为 FourSeasons。我想找到初始文件 C:/classical/vivaldi.mp3 的绝对路径,然后提供一个字符串“FourSeasons.mp3”,并将文件 C:/classical/vivaldi.mp3 更改为 C:/classical /四季.mp3。

我考虑过使用 renameTo 和 file writer,但这些都没有给我想要的结果。 RenameTo code :这会返回 false(重命名失败),并且往往会永久删除我的文件。

public static void main(String[] args) {
File mp3 = new File("C:/mp3.mp3");
boolean renamestatus = mp3.renameTo(new File("song.mp3"));
System.out.println(renamestatus);
}

我还尝试使用 FileReader 和 FileWriter 制作文件的精确副本,并使用新名称。此方法输出一个 mp3 文件,该文件会跳过并且听起来与输入文件相差甚远 这是我的 fileWriter 代码:

File inputFile = new File("C:/mp3.mp3");
File outputFile = new File("C:/song.mp3");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
  out.write(c);
in.close();
out.close();

Trying to take one mp3 file and rename it using a string variable. For example, I have a classical music folder, C:/classical, and I want a song called vivaldi renamed to FourSeasons. I want to find the absolute path of the initial file, C:/classical/vivaldi.mp3, then provide a string, "FourSeasons.mp3", and have the file C:/classical/vivaldi.mp3 changed to C:/classical/FourSeasons.mp3.

I've thought of using renameTo and file writer, but neither of these have given me desired results.
RenameTo code : this returns false (rename failed), and tends to permanently delete my file.

public static void main(String[] args) {
File mp3 = new File("C:/mp3.mp3");
boolean renamestatus = mp3.renameTo(new File("song.mp3"));
System.out.println(renamestatus);
}

I've also tried to use FileReader and FileWriter to make an exact copy of the file, with a new name. This method outputs an mp3 file that skips and is nowhere near sounding like the input file
This is my fileWriter code:

File inputFile = new File("C:/mp3.mp3");
File outputFile = new File("C:/song.mp3");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
  out.write(c);
in.close();
out.close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝颜夕 2024-11-18 13:45:56

我也遇到了同样的问题,

我使用新文件对象的绝对路径解决了它。

因此,在您的情况下:

boolean renamestatus = mp3.renameTo(new File("song.mp3"));

应该是

boolean renamestatus = mp3.renameTo(new File("C://song.mp3"));

其次,如果不使用绝对路径,您的文件不会被删除,但会以新名称移动到项目的根文件夹中。

I also faced same problem,

I solved it using Absolute path of the new file object.

So in your case :

boolean renamestatus = mp3.renameTo(new File("song.mp3"));

should be

boolean renamestatus = mp3.renameTo(new File("C://song.mp3"));

Secondly, without using Absolute path, your file doesn't get deleted, but it is moved to your project's root folder with new name.

别理我 2024-11-18 13:45:56

使用 renameTo 时,您需要指定文件所在的路径。例如,您使用的原始路径是“C:/mp3.mp3”,您希望 renameTo 中的路径看起来像“C” :/歌曲.mp3”。所以你的线路看起来像这样。

boolean renamestatus = mp3.renameTo(new File("C:/song.mp3"));

希望这对您有帮助。

When using renameTo you need to specify the path that you want the file to go to.For example with the one you are using the original path is "C:/mp3.mp3" you would want the path in renameTo to look like "C:/song.mp3". So your line would look something like this.

boolean renamestatus = mp3.renameTo(new File("C:/song.mp3"));

Hope that is helpful for you.

蓝海似她心 2024-11-18 13:45:56

FileReaderFileWriter 用于处理文本数据。

您的 MP3 文件是二进制数据,应该按二进制数据处理,因此您需要使用 FileInputStreamFileOutputStream 进行读/写。

FileReader and FileWriter are made to handle textual data only.

Your MP3 files are binary data and should be treated as such, so you'd need to use FileInputStream and FileOutputStream for reading/writing.

我不咬妳我踢妳 2024-11-18 13:45:56

尝试这样:

mp3.renameTo(new File("C:/song.mp3"));

您还可以看看这个 和这个答案。

Try like this:

mp3.renameTo(new File("C:/song.mp3"));

You can also have a look at this and this answers.

锦欢 2024-11-18 13:45:56

它不会删除原始文件,它工作正常,只需给出完整路径(源文件路径+新名称)

Its not deleting original, Its working fine, Just give complete path(Source file path + new name)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文