gpt4 book ai didi

java - 无法将 MP3 文件移动到其他文件夹

转载 作者:行者123 更新时间:2023-12-01 14:11:44 24 4
gpt4 key购买 nike

我尝试使用 File.renameTo() 将一些 MP3 文件移动到不同的文件夹,但它一直无法工作,我不知道为什么。

你能告诉我我做错了什么吗?

File songsFolder = new File("songs");
File[] songsList = songsFolder.listFiles();

for (int i = 0; i < allSongs.size(); i++) {
//allSongs is an ArrayList defined earlier
File music = (File) songsList[i];
FileInputStream fileMusic = new FileInputStream(music);
int size = (int) music.length();
fileMusic.skip(size - 128);
byte[] last128 = new byte[128];
fileMusic.read(last128);
String id3 = new String(last128);
String tag = id3.substring(0, 3);

if (musicsList[i].isFile()) {
File afile = songsList[i];
if (afile.renameTo(new File("songs/" + id3.substring(33, 62).trim() + "/" + songsList[i].getName()))) {
System.out.println("File moved successfully!");
} else {
System.out.println("File failed to move!");
}
}
}

输出为:

File failed to move!
File failed to move!
File failed to move!
File failed to move!

最佳答案

目录"songs/"+ id3.substring(33, 62).trim()已经存在吗? File.renameTo() 不会为您创建目录。

尝试这样的事情:

File afile = songsList[i];
File newDir = new File("songs", id3.substring(33, 62).trim());
newDir.mkdirs();
File newName = new File(newDir, afile.getName());
afile.renameTo(newName);

关于java - 无法将 MP3 文件移动到其他文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18486572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com