Java copy a folder excluding some internal file

前端 未结 4 664
故里飘歌
故里飘歌 2021-01-19 01:54

I have a folder with this structure

mainFolder

   --Sub1  
         --File .scl
         --File .awl
         --Other files
   --Sub2  
         --Fi         


        
4条回答
  •  攒了一身酷
    2021-01-19 02:16

    I think the ugliness comes from introducing ignoreFile(), which necessarily loses some of the useful information (which strings actually matter, which strings are file extensions, etc.) Additionally, that array is going to be created for every file in your hierarchy, which is extremely inefficient. Consider something like this, instead:

    FileUtils.copyDirectory(srcDir, dstDir, new FileFilter() {
        public boolean accept(File pathname) {
            // We don't want 'Sub3' folder to be imported
            // + look at the settings to decide if some format needs to be
            // excluded
            String name = pathname.getName();
            if (!Settings.getSiemensOptionAWL() && name.endsWith(".awl"))
                return false;
            if (!Settings.getSiemensOptionSCL() && name.endsWith(".scl"))
                return false;
    
            return !(name.equals("Sub3") && pathname.isDirectory());
        }
    }, true);
    

提交回复
热议问题