FileHandler
com.jsmacrosce.jsmacros.core.library.impl.classes.FileHandler
Represents a file and charset for script file operations. Obtain handlers with FS.open;
each operation opens and closes its own file resource, except readLines() and streamBytes(), whose returned resources remain open until closed. The read and write methods distinguish whole-file, streaming, text, and byte operations; writes create the file if needed but do not create parent directories.
js
// FS.open resolves the path against this script's own folder
const notes = FS.open("notes/log.txt");
// write replaces the file, append adds to the end
notes.write("first run\n");
notes.append("second run\n");
// read and readBytes are the whole file at once, and readLines streams it,
// so the iterator is the one that has to be closed
print(notes.read());
const lines = notes.readLines();
try {
while (lines.hasNext()) {
print(lines.next());
}
} finally {
lines.close();
}Nested types
Constructors
new FileHandler
Sets up a handler for a path using UTF-8 for text operations. The path is used as a Java file path, not rebased against the script folder; no file is opened or created here.
Parameters:
path: file path, interpreted relative to the Java process working directory when relative
Sets up a handler for a path using the named charset for text operations. The path is not rebased against the script folder, and no file is opened or created here; an invalid or unsupported charset name fails during construction.
Parameters:
path: file path, interpreted relative to the Java process working directory when relativecharset: charset name resolved by Charset.forName(String)
Sets up a handler for the supplied file using the named charset for text operations. No file is opened or created here; an invalid or unsupported charset name fails during construction.
Parameters:
path: file associated with the handlercharset: charset name resolved by Charset.forName(String)
Sets up a handler for a path using the supplied charset for text operations. The path is not rebased against the script folder, and no file is opened or created here.
Parameters:
path: file path, interpreted relative to the Java process working directory when relativecharset: charset to use for text operations
Sets up a handler for the supplied file using UTF-8 for text operations. No file is opened or created here.
Parameters:
path: file associated with the handler
Methods
append
Appends text encoded with this handler's charset, creating the file if it does not exist.
Parameters:
s: the text to add, encoded in the handler's charset
Returns: self for chaining
Appends bytes unchanged; the handler's charset is not used. Creates the file if it does not exist.
Parameters:
b: the bytes to add
Returns: self for chaining
getFile
read
readBytes
Reads the whole file into a byte array without decoding. Files larger than
Integer.MAX_VALUE bytes are rejected with IOException.Returns: the whole file, or an empty array for an empty one
readLines
Returns a line iterator decoded with this handler's charset. The reader remains open until the iterator is closed; close it when finished. Opening failures and later read failures are wrapped in RuntimeException.
Returns: an iterator over the lines, which is also closeable
streamBytes
Returns a buffered stream of the file's raw bytes. The stream keeps the file open until closed; close it when finished. Opening failures are wrapped in RuntimeException.
Returns: a stream over the file's bytes, which the caller has to close
toString
write
Replaces the file contents with text encoded using this handler's charset. Creates the file if needed, but does not create parent directories.
Parameters:
s: the text to write, encoded in the handler's charset
Returns: self for chaining
Replaces the file contents with the supplied bytes, unchanged and without using the handler's charset. Creates the file if needed, but does not create parent directories.
Parameters:
b: the bytes to write
Returns: self for chaining