java
15 lines · 5 steps
Expanding glob patterns with NIO
Turn a glob string into a sorted list of matching paths by walking a directory tree and testing each entry.
Explained by
highlit
1public List<Path> expandGlob(Path baseDir, String glob) throws IOException {
2 FileSystem fs = baseDir.getFileSystem();
3 PathMatcher matcher = fs.getPathMatcher("glob:" + glob);
4 boolean recursive = glob.contains("**");
5 int maxDepth = recursive ? Integer.MAX_VALUE : 1;
6
7 List<Path> matches = new ArrayList<>();
8 try (Stream<Path> paths = Files.walk(baseDir, maxDepth)) {
9 paths.filter(path -> !path.equals(baseDir))
10 .filter(path -> matcher.matches(baseDir.relativize(path)))
11 .sorted()
12 .forEach(matches::add);
13 }
14 return matches;
15}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A PathMatcher lets you reuse compiled glob logic instead of hand-rolling wildcard parsing.
- 2Matching relative paths keeps glob semantics independent of the absolute location of the base directory.
- 3Streams from Files.walk hold OS resources, so closing them in try-with-resources prevents descriptor leaks.
Related explainers
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
Intermediate
8 steps
java
public final class EncodingDetector { public enum Encoding { UTF_8, UTF_16LE, UTF_16BE, UTF_32LE, UTF_32BE, ASCII, UNKNOWN
Detecting text encoding from raw bytes in Java
byte-manipulation
encoding-detection
bitwise-operations
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/expanding-glob-patterns-with-nio-explained-java-3808/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.