NIO extensions for Apache Groovy providing enhanced file system operations and path handling
npx @tessl/cli install tessl/maven-org-codehaus-groovy--groovy-nio@3.0.0Groovy NIO Extensions provides enhanced file system operations and path handling capabilities for Apache Groovy. This package extends Groovy's existing I/O functionality with modern Java NIO.2 features, enabling more efficient file system access, path manipulation, and stream operations with Groovy's characteristic closure-based programming model.
implementation 'org.codehaus.groovy:groovy-nio:3.0.25'<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-nio</artifactId>
<version>3.0.25</version>
</dependency>import java.nio.file.Path
import java.nio.file.Paths
// Extension methods are automatically available on Path objectsimport java.nio.file.Path
import java.nio.file.Paths
// Create a path
Path myFile = Paths.get("example.txt")
// Write text to file
myFile.text = "Hello, World!"
// Read text from file
String content = myFile.getText()
// Append text
myFile << " Additional content"
// Process lines with closure
myFile.eachLine { line ->
println line
}
// Directory traversal
Path myDir = Paths.get("my-directory")
myDir.eachFile { file ->
println "Found: ${file.fileName}"
}Groovy NIO Extensions is built around several key components:
NioExtensions class provides static extension methods that are automatically applied to java.nio.file.Path objectsWritable interface for streaming operationsCore file I/O operations for reading and writing text, binary data, and objects to files. Provides both simple text operations and advanced charset handling.
// Text operations
String getText(Path self);
String getText(Path self, String charset);
void write(Path self, String text);
void write(Path self, String text, String charset);
void setText(Path self, String text);
// Binary operations
byte[] getBytes(Path self);
byte[] readBytes(Path self);
void setBytes(Path self, byte[] bytes);
// Line operations
List<String> readLines(Path self);
List<String> readLines(Path self, String charset);Append data to existing files with support for text, binary data, and various input sources including streams and closures.
// Text appending
void append(Path self, Object text);
void append(Path self, Object text, String charset);
Path leftShift(Path self, Object text);
// Binary appending
void append(Path self, byte[] bytes);
void append(Path self, InputStream stream);
Path leftShift(Path self, byte[] bytes);Process files line-by-line with closures, including splitting, filtering, and transformation operations with charset support.
<T> T eachLine(Path self, Closure<T> closure);
<T> T eachLine(Path self, String charset, Closure<T> closure);
<T> T splitEachLine(Path self, String regex, Closure<T> closure);
Writable filterLine(Path self, Closure closure);Traverse directory structures with filtering, sorting, and recursion options. Support for file type filtering and advanced traversal patterns.
void eachFile(Path self, Closure closure);
void eachFile(Path self, FileType fileType, Closure closure);
void eachFileRecurse(Path self, Closure closure);
void traverse(Path self, Map<String, Object> options, Closure closure);Create and manage various stream types with automatic resource management using closures for safe operation.
BufferedReader newReader(Path self);
BufferedWriter newWriter(Path self);
<T> T withReader(Path self, Closure<T> closure);
<T> T withWriter(Path self, Closure<T> closure);
BufferedInputStream newInputStream(Path self);
BufferedOutputStream newOutputStream(Path self);Handle Java object serialization and deserialization with support for custom class loaders and closure-based processing.
ObjectInputStream newObjectInputStream(Path self);
ObjectOutputStream newObjectOutputStream(Path self);
<T> T withObjectInputStream(Path self, Closure<T> closure);
void eachObject(Path self, Closure closure);Convert Path objects to Groovy's Writable interface for streaming and output operations.
Path asWritable(Path self);
Path asWritable(Path self, String encoding);
<T> T asType(Path path, Class<T> c);// Core Groovy types used throughout the API
interface Closure<T> {
T call(Object... args);
}
// File type enumeration for filtering
enum FileType {
FILES, // Regular files only
DIRECTORIES, // Directories only
ANY // Both files and directories
}
// Writable interface for streaming
interface Writable {
Writer writeTo(Writer out);
}
// WritablePath - Path wrapper implementing Writable
class WritablePath implements Path, Writable {
WritablePath(Path delegate);
WritablePath(Path delegate, String encoding);
Writer writeTo(Writer out);
}