Skip to content

Instantly share code, notes, and snippets.

@joakime
Created August 4, 2022 22:02
Show Gist options
  • Save joakime/038984edd4fc88f2de74c06454e3e46c to your computer and use it in GitHub Desktop.
Save joakime/038984edd4fc88f2de74c06454e3e46c to your computer and use it in GitHub Desktop.
Walk all of JRT with Java NIO FileSystem
package fs;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
public class JrtDemo
{
public static void main(String[] args) throws IOException
{
URI jrtRoot = URI.create("jrt:/");
Map<String,?> env = Map.of();
try (FileSystem fs = FileSystems.newFileSystem(jrtRoot, env))
{
Path root = fs.getPath("/");
try (Stream<Path> walkStream = Files.walk(root))
{
final AtomicInteger i = new AtomicInteger(0);
walkStream.forEach((path) ->
{
String type = "";
if (Files.isDirectory(path))
type = "[dir]";
System.out.printf("[%d] %s %s%n", i.incrementAndGet(), path.toUri(), type);
});
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment