Two Java examples to show you how to list files in a directory :

  1. For Java 8, Files.walk
  2. Before Java 8, create a recursive loop to list all files.

1. Files.walk

1.1 List all files.

	try (Stream<Path> walk = Files.walk(Paths.get("C:\\projects"))) {

		List<String> result = walk.filter(Files::isRegularFile)
				.map(x -> x.toString()).collect(Collectors.toList());

		result.forEach(System.out::println);

	} catch (IOException e) {
		e.printStackTrace();
	}

1.2 List all folders.

	try (Stream<Path> walk = Files.walk(Paths.get("C:\\projects"))) {

		List<String> result = walk.filter(Files::isDirectory)
				.map(x -> x.toString()).collect(Collectors.toList());

		result.forEach(System.out::println);

	} catch (IOException e) {
		e.printStackTrace();
	}

1.3 List all files end with .java

	try (Stream<Path> walk = Files.walk(Paths.get("C:\\projects"))) {

		List<String> result = walk.map(x -> x.toString())
				.filter(f -> f.endsWith(".java")).collect(Collectors.toList());

		result.forEach(System.out::println);

	} catch (IOException e) {
		e.printStackTrace();
	}

1.4 Find a file – HeaderAnalyzer.java

	try (Stream<Path> walk = Files.walk(Paths.get("C:\\projects"))) {

		List<String> result = walk.map(x -> x.toString())
				.filter(f -> f.contains("HeaderAnalyzer.java"))
				.collect(Collectors.toList());

		result.forEach(System.out::println);

	} catch (IOException e) {
		e.printStackTrace();
	}

2. Classic

In the old days, we can create a recursive loop to implement the search file function like this :

2.1 List all files end with .java

DirectoryWalk.java

package com.favtuts.io.directory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class DirectoryWalk {    

    public static void main(String[] args) {

        final File folder = new File("/home/tvt/workspace/favtuts/test");
        List<String> result = new ArrayList<>();

        search(".*\\.log", folder, result);

        for (String s : result) {
            System.out.println(s);
        }
                
    }

    public static void search(final String pattern, final File folder, List<String> result) {
        for (final File f : folder.listFiles()) {

            if (f.isDirectory()) {
                search(pattern, f, result);
            }

            if (f.isFile()) {
                if (f.getName().matches(pattern)) {
                    result.add(f.getAbsolutePath());
                }
            }
        }
    }

}

Output

/home/tvt/workspace/favtuts/test/test-a1.log
/home/tvt/workspace/favtuts/test/test-a2.log
/home/tvt/workspace/favtuts/test/test-b/test-d/test-d1.log
/home/tvt/workspace/favtuts/test/test-b/test-d/test-d2.log
/home/tvt/workspace/favtuts/test/test-b/test-c/test-c2.log
/home/tvt/workspace/favtuts/test/test-b/test-c/test-c1.log

Download Source Code

$ git clone https://github.com/favtuts/java-core-tutorials-examples

$ cd java-io/directory

References

  1. Java docs – Files.walk
  2. Java Files.walk examples

Leave a Reply

Your email address will not be published. Required fields are marked *