Skip to content

Instantly share code, notes, and snippets.

@ijokarumawak
Created November 30, 2018 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ijokarumawak/59ac7c096b52f7c0d0a2f72cad981bc6 to your computer and use it in GitHub Desktop.
Save ijokarumawak/59ac7c096b52f7c0d0a2f72cad981bc6 to your computer and use it in GitHub Desktop.
Simple Java code to list files using NIO while waiting user input to proceed. This can be useful to see how it makes OS/native API calls with strace command.
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Scanner;
public class ListFile {
private static void waitUserInput(Scanner sc) {
System.out.println("Press enter key to proceed.");
sc.nextLine();
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
waitUserInput(sc);
System.out.println("Start Listing");
Path path = new File("/tmp").toPath();
Stream<Path> inputStream = Files.find(path, 1, (p, attr) -> {
System.out.printf("p=%s, attr=%s\n", p, attr);
waitUserInput(sc);
return true;
});
List<Path> paths = inputStream.collect(Collectors.toList());
System.out.printf("paths=%s\n", paths);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment