Update : Move files from one folder to another based on file extension

后端 未结 3 1297
耶瑟儿~
耶瑟儿~ 2021-01-25 12:46

Situation:

I am doing an automation where I have to download only CSV files from a set of files. And now i want to move only CSV files from one folder t

3条回答
  •  我在风中等你
    2021-01-25 13:06

    Just add pathname.deleteOnExit(); on accept method

    @Override
     public boolean accept(File pathname) {
           boolean source = pathname.getName().toLowerCase().endsWith(".csv");
    
           if (source) {
               pathname.deleteOnExit();
               return true;
           }
       return false;
     }
    

    whole code :

    import org.apache.commons.io.FileUtils;
    
    import java.io.File;
    import java.io.FileFilter;
    import java.io.IOException;
    
    /**
     * Created by Lenovo on 02/12/2018.
     */
    public class FileMove {
        public static void main(String a[])
    
        {
            try {
    
                File source = new File("C:\\Users\\sh370472\\Downloads");
                File dest = new File("E:\\Query\\");
    
                FileUtils.copyDirectory(source, dest, new FileFilter() {
    
                    @Override
                    public boolean accept(File pathname) {
                        boolean source = pathname.getName().toLowerCase().endsWith(".csv");
    
                        if (source) {
                            pathname.deleteOnExit();
                            return true;
                        }
                        return false;
                    }
    
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

提交回复
热议问题