Created
November 30, 2018 03:34
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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