Java File 对象的 renameTo 方法删除我的文件而不是重命名它。威斯塔64
尝试获取一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我也遇到了同样的问题,
我使用新文件对象的绝对路径解决了它。
因此,在您的情况下:
应该是
其次,如果不使用绝对路径,您的文件不会被删除,但会以新名称移动到项目的根文件夹中。
I also faced same problem,
I solved it using Absolute path of the new file object.
So in your case :
should be
Secondly, without using Absolute path, your file doesn't get deleted, but it is moved to your project's root folder with new name.
使用 renameTo 时,您需要指定文件所在的路径。例如,您使用的原始路径是“C:/mp3.mp3”,您希望 renameTo 中的路径看起来像“C” :/歌曲.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.
Hope that is helpful for you.
FileReader
和FileWriter
用于仅处理文本数据。您的 MP3 文件是二进制数据,应该按二进制数据处理,因此您需要使用
FileInputStream
和FileOutputStream
进行读/写。FileReader
andFileWriter
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
andFileOutputStream
for reading/writing.尝试这样:
您还可以看看这个 和这个答案。
Try like this:
You can also have a look at this and this answers.
它不会删除原始文件,它工作正常,只需给出完整路径(源文件路径+新名称)
Its not deleting original, Its working fine, Just give complete path(Source file path + new name)