简体   繁体   English

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

[英]Java File object's renameTo method deletes my file instead of renaming it. Vistax64

Trying to take one mp3 file and rename it using a string variable.试图获取一个 mp3 文件并使用字符串变量重命名它。 For example, I have a classical music folder, C:/classical, and I want a song called vivaldi renamed to FourSeasons.例如,我有一个古典音乐文件夹 C:/classical,我想要一首名为 vivaldi 的歌曲重命名为 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 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 /四季.mp3。

I've thought of using renameTo and file writer, but neither of these have given me desired results.我曾想过使用 renameTo 和文件编写器,但这些都没有给我想要的结果。 RenameTo code: this returns false (rename failed), and tends to permanently delete my file. RenameTo 代码:这将返回 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);
}

I've also tried to use FileReader and FileWriter to make an exact copy of the file, with a new name.我还尝试使用 FileReader 和 FileWriter 以新名称制作文件的精确副本。 This method outputs an mp3 file that skips and is nowhere near sounding like the input file This is my fileWriter code:此方法输出一个 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();

I also faced same problem,我也面临同样的问题,

I solved it using Absolute path of the new file object.我使用新文件 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.其次,如果不使用绝对路径,您的文件不会被删除,而是会以新名称移动到项目的根文件夹中。

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".使用 renameTo 时,您需要指定要将文件转换为 go 的路径。例如,您使用的原始路径是“C:/mp3.mp3”,您希望 renameTo 中的路径看起来像“C :/歌曲.mp3”。 So your line would look something like this.所以你的线看起来像这样。

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

Hope that is helpful for you.希望这对你有帮助。

Try like this:试试这样:

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

You can also have a look at this and this answers.你也可以看看这个这个答案。

FileReader and FileWriter are made to handle textual data only . FileReaderFileWriter用于处理文本数据。

Your MP3 files are binary data and should be treated as such, so you'd need to use FileInputStream and FileOutputStream for reading/writing.您的 MP3 文件是二进制数据,应该这样处理,因此您需要使用FileInputStreamFileOutputStream进行读/写。

Its not deleting original, Its working fine, Just give complete path(Source file path + new name)它没有删除原始文件,它工作正常,只需给出完整路径(源文件路径+新名称)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM