Skip to content

FileHandler ​

com.jsmacrosce.jsmacros.core.library.impl.classes.FileHandler

Since1.1.8

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 ​

new FileHandler(String path)#
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
new FileHandler(String path, String charset)#
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 relative
  • charset: charset name resolved by Charset.forName(String)
new FileHandler(File path, String charset)#
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:

new FileHandler(String path, Charset charset)#
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 relative
  • charset: charset to use for text operations
new FileHandler(File path)#
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
new FileHandler(File path, Charset charset)#
Sets up a handler for the supplied file and charset for later text operations; no file is opened or created here.

Parameters:

  • path: file associated with the handler
  • charset: charset to use for text operations

Methods ​

append ​

append(String s): FileHandlerSince1.1.8#
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
append(byte[] b): FileHandlerSince1.2.6#
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 ​

getFile(): File#
Returns the File object associated with this handler.
Returns: the file object supplied to or created for this handler

read ​

read(): StringSince1.1.8#
Reads the whole file into memory and decodes it using this handler's charset.
Returns: the whole file, or an empty string for an empty one

readBytes ​

readBytes(): byte[]Since1.2.6#
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 ​

readLines(): FileHandler.FileLineIteratorSince1.8.4#
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 ​

streamBytes(): BufferedInputStreamSince1.8.4#
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 ​

toString(): String#
Returns a diagnostic representation containing the file's absolute path; it is not JSON serialization.
Returns: diagnostic representation containing the file's absolute path

write ​

write(String s): FileHandlerSince1.1.8#
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
write(byte[] b): FileHandlerSince1.1.8#
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