Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Iterate over all files in directory

I want to find all the txt files in directory and in the nested sub-directories. If found, I want to move that from one location to another.

The below code works fine, if i don't have any nested sub-directories.

The problem with the below code is, Once it find the nested directories it return the file only from that particular nested sub-directory. But I want all the txt files in my directory ( parent and its nested sub-directories ).

public class FilesFindingInDirectory {
    static ArrayList<File> al = new ArrayList<File>();
    static File fileLocation = null;
    public static void main(String[] args) throws IOException {


        File filePath = new File("C:\\Users\\Downloads");

        File[] listingAllFiles = filePath.listFiles();

        ArrayList<File> allFiles = iterateOverFiles(listingAllFiles);


                for (File file : allFiles) {
                    if(file != null) {
                        String fileName = file.getName();

                        String sourceFilepath = file.getAbsolutePath();
                        File targetFilePath = new File("D:\\TestFiles");
                        String targetPath = targetFilePath.getPath();

                        Files.move(Paths.get(sourceFilepath), Paths.get("D:\\TestFiles\\" + fileName)); 
                    }

                }
            }


public static ArrayList<File> iterateOverFiles(File[] files) {


        for (File file : files) {

            if (file.isDirectory()) {

                iterateOverFiles(file.listFiles());// Calls same method again.

            } else {

                fileLocation = findFileswithTxtExtension(file);
                if(fileLocation != null) {
                    System.out.println(fileLocation);
                    al.add(fileLocation);
                }


            }
        }

        return al;
    }

public static File findFileswithTxtExtension(File file) {

        if(file.getName().toLowerCase().endsWith("txt")) {
            return file;
        }

        return null;
    }
}
like image 810
Aishu Avatar asked Apr 14 '18 18:04

Aishu


1 Answers

You're already using the nio Files API to move the files, why not using it to iterate over the files?

 List<Path> txtFiles = Files.walk(Paths.get("C:\\Users\\Downloads"))
                            //use to string here, otherwise checking for path segments
                            .filter(p -> p.toString().endsWith(".txt"))
                            .collect(Collectors.toList());

If you don't need that intermediary list, you could as well run your move operation in a foreach terminal operation

Files.walk(Paths.get("C:\\Users\\Downloads"))
     .filter(p -> p.toString().endsWith(".txt"))
     .forEach(p -> {
        try {
            Files.move(p, Paths.get("D:\\TestFiles", p.getFileName().toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
like image 158
Gerald Mücke Avatar answered Sep 18 '22 04:09

Gerald Mücke