Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文件复制到java中的特定目录中_Java_File_Nio - Fatal编程技术网

将文件复制到java中的特定目录中

将文件复制到java中的特定目录中,java,file,nio,Java,File,Nio,下面是将文件从一个目录复制到另一个目录的代码。 例如:如果文件名是Red ready over,Red ready overflow,NEFFEX Destiny,那么它应该创建一个目录名Red并将文件复制到其中, 对于另一位艺术家,它应该将NEFFEX文件夹和文件复制到其中 问题是,如果对Files.copy进行了注释,它可以创建目录。但是,如果对Files.copy未进行注释,它无法创建目录,而是复制文件。 该文件不可播放,因为它没有扩展名。文件复制不正确 import java.io.Fi

下面是将文件从一个目录复制到另一个目录的代码。
例如:如果文件名是Red ready over,Red ready overflow,NEFFEX Destiny,那么它应该创建一个目录名Red并将文件复制到其中, 对于另一位艺术家,它应该将NEFFEX文件夹和文件复制到其中

问题是,如果对Files.copy进行了注释,它可以创建目录。但是,如果对Files.copy未进行注释,它无法创建目录,而是复制文件。 该文件不可播放,因为它没有扩展名。文件复制不正确

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:\Users\Fawkes\Music\Music\Red已经结束 C:\Users\Fawkes\Music\Music\Red已过流 C:\Users\Fawkes\Music\Music\NEFFEX Destiny

目的地: C:\输出\

例如: C:\Output\Red\已结束 C:\Output\Red\已过流 C:\Output\NEFFEX\Destiny

它被声明为变量:path和target_path


功能:在这里粘贴代码时,行号应该在那里就好了。

我对你的描述没有任何意义。您引用的文件名没有路径分隔符。我假设您没有单元测试来告诉我们代码应该做什么//获取文件总数这一行加载该目录中的所有文件,我调试了它并看到了它。您引用的文件名没有路径分隔符-->我无法理解您的确切意思?红色已过,红色已过,NEFFEX Destiny看起来不像文件名。指向和来自的绝对路径是什么?如果问题是无法创建目录,请将dir.mkdir替换为Files.createDirectorydir.toPath,这将始终成功或引发IOException。事实上,您根本不应该使用File类,因为它已经过时了,原因正是:File的许多方法不能正确地报告错误。请改用路径和文件。@AbhijitSarkar文件名路径为:Source:C:\\Users\\Fawkes\\Music\\Music\\Red已过C:\\Users\\Fawkes\\Music\\Music\\Red已过流C:\\Users\\Fawkes\\Music\\NEFFEX Destination:C:\\Output\\Red\\ready over C:\\Output\\Red\\ready over过流C:\\Output\\NEFFEX\\DENITY它被声明为变量:path和target\U path
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
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()) {
            Path src=Paths.get(total_file[i].getPath());
            Path dest=Paths.get(dir.getAbsolutePath().concat("\\").concat(total_file[i].getName()));
            Files.copy(src, dest ,StandardCopyOption.REPLACE_EXISTING);
            System.out.println("Files copied: "+(i+1)+"/"+total_file.length);
        }
        else {
            dir.mkdir();    
            Path src=Paths.get(total_file[i].getPath());
            Path dest=Paths.get(dir.getAbsolutePath().concat("\\").concat(total_file[i].getName()));// only needed to add this line.
            Files.copy(src, dest ,StandardCopyOption.REPLACE_EXISTING);             
            System.out.println("Files copied: "+(i+1)+"/"+total_file.length);
        }
    }   


}
public static void main(String[] args) {
    try {
        new OrgLogic();
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}