代码之家  ›  专栏  ›  技术社区  ›  Fawkes

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

  •  -3
  • Fawkes  · 技术社区  · 8 年前

    下面是将文件从一个目录复制到另一个目录的代码。 例如:如果文件名是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

    (特点:在这里粘贴代码时 生产线编号 应该在那里。)

    1 回复  |  直到 8 年前
        1
  •  0
  •   Fawkes    8 年前
    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();
        }
      }
    }