NIO extensions for Apache Groovy providing enhanced file system operations and path handling
—
Core file I/O operations for reading and writing text, binary data, and objects to files. Provides both simple text operations and advanced charset handling with optional BOM support.
Read file content as strings with optional charset specification.
/**
* Read the content of the Path and returns it as a String using default charset
* @param self the file whose content we want to read
* @return a String containing the content of the file
* @throws IOException if an IOException occurs
*/
String getText(Path self);
/**
* Read the content of the Path using the specified encoding and return it as a String
* @param self the file whose content we want to read
* @param charset the charset used to read the content of the file
* @return a String containing the content of the file
* @throws IOException if an IOException occurs
*/
String getText(Path self, String charset);
/**
* Reads the file into a list of Strings, with one item for each line
* @param self a Path
* @return a List of lines
* @throws IOException if an IOException occurs
*/
List<String> readLines(Path self);
/**
* Reads the file into a list of Strings, with one item for each line
* @param self a Path
* @param charset opens the file with a specified charset
* @return a List of lines
* @throws IOException if an IOException occurs
*/
List<String> readLines(Path self, String charset);Usage Examples:
import java.nio.file.Path
import java.nio.file.Paths
Path file = Paths.get("data.txt")
// Read entire file as string
String content = file.getText()
// Read with specific charset
String content = file.getText("UTF-8")
// Read as lines
List<String> lines = file.readLines()
// Read lines with charset
List<String> lines = file.readLines("UTF-8")
// Using property syntax (Groovy feature)
String content = file.textRead file content as byte arrays for binary data handling.
/**
* Read the content of the Path and returns it as a byte[]
* @param self the file whose content we want to read
* @return a byte array containing the content of the file
* @throws IOException if an IOException occurs
*/
byte[] getBytes(Path self);
/**
* Reads the content of the file into a byte array
* @param self a Path
* @return a byte array with the contents of the file
* @throws IOException if an IOException occurs
*/
byte[] readBytes(Path self);Usage Examples:
Path binaryFile = Paths.get("image.png")
// Read as byte array
byte[] data = binaryFile.getBytes()
// or
byte[] data = binaryFile.readBytes()
// Using property syntax
byte[] data = binaryFile.bytesWrite text content to files with charset and BOM support.
/**
* Write the text to the Path without writing a BOM
* @param self a Path
* @param text the text to write to the Path
* @throws IOException if an IOException occurs
*/
void write(Path self, String text);
/**
* Write the text to the Path with BOM option
* @param self a Path
* @param text the text to write to the Path
* @param writeBom whether to write the BOM
* @throws IOException if an IOException occurs
*/
void write(Path self, String text, boolean writeBom);
/**
* Write the text to the Path using the specified encoding without writing a BOM
* @param self a Path
* @param text the text to write to the Path
* @param charset the charset used
* @throws IOException if an IOException occurs
*/
void write(Path self, String text, String charset);
/**
* Write the text to the Path using the specified encoding with BOM option
* @param self a Path
* @param text the text to write to the Path
* @param charset the charset used
* @param writeBom whether to write a BOM
* @throws IOException if an IOException occurs
*/
void write(Path self, String text, String charset, boolean writeBom);
/**
* Synonym for write(text) allowing file.text = 'foo'
* @param self a Path
* @param text the text to write to the Path
* @throws IOException if an IOException occurs
*/
void setText(Path self, String text);
/**
* Synonym for write(text, charset)
* @param self A Path
* @param text The text to write to the Path
* @param charset The charset used when writing to the file
* @throws IOException if an IOException occurs
*/
void setText(Path self, String text, String charset);Usage Examples:
Path file = Paths.get("output.txt")
// Simple text writing
file.write("Hello, World!")
// Using property syntax
file.text = "Hello, World!"
// Write with charset
file.write("Hello, World!", "UTF-8")
file.setText("Hello, World!", "UTF-8")
// Write with BOM for UTF-16
file.write("Hello, World!", "UTF-16LE", true)Write binary data to files.
/**
* Write the bytes from the byte array to the Path
* @param self the file to write to
* @param bytes the byte[] to write to the file
* @throws IOException if an IOException occurs
*/
void setBytes(Path self, byte[] bytes);Usage Examples:
Path binaryFile = Paths.get("output.bin")
byte[] data = [0x48, 0x65, 0x6C, 0x6C, 0x6F] as byte[]
// Write byte array
binaryFile.setBytes(data)
// Using property syntax
binaryFile.bytes = dataGet file size information.
/**
* Provide the standard Groovy size() method for Path
* @param self a Path object
* @return the file's size (length)
* @throws IOException if an IOException occurs
*/
long size(Path self);Usage Examples:
Path file = Paths.get("data.txt")
// Get file size
long fileSize = file.size()Install with Tessl CLI
npx tessl i tessl/maven-org-codehaus-groovy--groovy-nio