下面是将文件从一个目录复制到另一个目录的代码。
例如:如果文件名是Red ready over,Red ready over(slow),NEFFEX Destiny,那么它应该创建一个目录名Red并将文件复制到其中,
对于另一位艺术家,应该将NEFFEX文件夹中的文件复制到其中。
问题是它可以创建目录if文件。副本已注释。但它无法创建dir,只能在创建文件时复制文件。副本未注释。
该文件无法播放,因为它没有扩展名(似乎没有正确复制该文件)。
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
public class OrgLogic {
String path="C:\\Users\\Fawkes\\Music\\Music\\";
String target_path="C:\\Output\\";
OrgLogic() throws IOException{
File f=new File(path); //loads the input dir path
File dir=new File(target_path); //loads the output dir path
dir.mkdir();//create a new dir name output
File[] total_file=f.listFiles();//get the total number of file
//System.out.println(total_file.length);//prints the total number of the file
for(int i=0;i<total_file.length;i++) {
String name=total_file[i].getName();
String new_name=name.substring(0, name.indexOf("-")-1);
dir=new File(target_path+new_name);
if(dir.exists()) {
//new File(new_path+new_name).mkdir();
Files.copy(total_file[i].toPath(), dir.toPath(),StandardCopyOption.REPLACE_EXISTING);
}
else {
//new File(target_path+new_name).mkdir();
dir.mkdir();
Files.copy(total_file[i].toPath(), dir.toPath(),StandardCopyOption.REPLACE_EXISTING);
}
}
}
public static void main(String[] args) {
try {
new OrgLogic();
} catch (IOException e) {
e.printStackTrace();
}
}
}
文件名路径为:
资料来源:
C: \用户\福克斯\音乐\音乐\红色已经结束
C: \用户\福克斯\音乐\音乐\红色已结束(慢速)
C: \Users\Fawkes\Music\Music\NEFFEX Destiny
目的地:
C: \输出\
对于eg:
C: \输出\红色\已结束
C: \输出\红色\已结束(慢速)
C: \输出\NEFFEX\Destiny
它被声明为变量:path和target\u path
(特点:在这里粘贴代码时
生产线编号
应该在那里。)