API: files
Source:
io/files.ts
Provides a comprehensive static façade for file and directory operations, abstracting the underlying Java file system implementation.
Usage
import { files } from "sdk/io";
let tempFile = files.createTempFile("temp", ".txt");
console.log("Temp file: " + tempFile);
files.writeText(tempFile, "Some text");
files.deleteFile(tempFile);Classes
Files
The Files class provides static methods for high-level file system manipulation,
including checking properties, reading/writing content, and navigating the structure.
Methods
exists
exists (path:string):booleanChecks if a file or directory exists at the given path.
@param path The path to check.
@returns True if the path exists, false otherwise.
isExecutable
isExecutable (path:string):booleanChecks if the file or directory at the given path is executable.
@param path The path to check.
@returns True if executable, false otherwise.
isReadable
isReadable (path:string):booleanChecks if the file or directory at the given path is readable.
@param path The path to check.
@returns True if readable, false otherwise.
isWritable
isWritable (path:string):booleanChecks if the file or directory at the given path is writable.
@param path The path to check.
@returns True if writable, false otherwise.
isHidden
isHidden (path:string):booleanChecks if the file or directory at the given path is hidden.
@param path The path to check.
@returns True if hidden, false otherwise.
isDirectory
isDirectory (path:string):booleanChecks if the path refers to a directory.
@param path The path to check.
@returns True if it's a directory, false otherwise.
isFile
isFile (path:string):booleanChecks if the path refers to a regular file.
@param path The path to check.
@returns True if it's a file, false otherwise.
isSameFile
isSameFile (path1:string, path2:string):booleanChecks if two paths refer to the same underlying file system object.
@param path1 The first path.
@param path2 The second path.
@returns True if they reference the same file/directory, false otherwise.
getCanonicalPath
getCanonicalPath (path:string):stringReturns the canonical (absolute and normalized) path for the given path.
@param path The path to normalize.
@returns The canonical path string.
getName
getName (path:string):stringGets the simple name of the file or directory at the given path (the last element).
@param path The path.
@returns The name.
getParentPath
getParentPath (path:string):stringGets the path of the parent directory.
@param path The path.
@returns The parent path string, or null/empty if none exists.
readBytes
readBytes (path:string):any[]Reads all bytes from a file into a JavaScript byte array (an array of numbers).
Note: This method automatically converts the native Java byte array to a
JavaScript array usingBytes.toJavaScriptBytes().
@param path The path to the file.
@returns A JavaScript array of byte values.
readBytesNative
readBytesNative (path:string):any[]Reads all bytes from a file and returns the native Java byte array object.
@param path The path to the file.
@returns The native Java byte array.
readText
readText (path:string):stringReads all text content from a file using the platform's default character encoding.
@param path The path to the file.
@returns The content of the file as a string.
writeBytes
writeBytes (path:string, data:any[]):voidWrites the content of a JavaScript byte array to a file. Overwrites existing content.
Note: This method automatically converts the JavaScript array to a native
Java byte array usingBytes.toJavaBytes()before writing.
@param path The path to the file.
@param data The JavaScript array of byte values to write.
writeBytesNative
writeBytesNative (path:string, data:any[]):voidWrites the content of a native Java byte array to a file. Overwrites existing content.
@param path The path to the file.
@param data The native Java byte array to write.
writeText
writeText (path:string, text:string):voidWrites a string of text to a file using the platform's default character encoding. Overwrites existing content.
@param path The path to the file.
@param text The string content to write.
getLastModified
getLastModified (path:string):DateGets the last modified time of the file or directory.
@param path The path to the file or directory.
@returns A JavaScript Date object representing the last modified time.
setLastModified
setLastModified (path:string, time:Date):voidSets the last modified time of the file or directory.
@param path The path to the file or directory.
@param time The new Date object to set as the last modified time.
getOwner
getOwner (path:string):stringGets the owner of the file or directory.
@param path The path to the file or directory.
@returns The owner name as a string.
setOwner
setOwner (path:string, owner:string):voidSets the owner of the file or directory.
@param path The path to the file or directory.
@param owner The new owner name.
getPermissions
getPermissions (path:string):stringGets the permissions string for the file or directory (implementation dependent).
@param path The path to the file or directory.
@returns The permissions string.
setPermissions
setPermissions (path:string, permissions:string):voidSets the permissions for the file or directory (implementation dependent).
@param path The path to the file or directory.
@param permissions The permissions string.
size
size (path:string):numberGets the size of the file in bytes.
@param path The path to the file.
@returns The size in bytes.
createFile
createFile (path:string):voidCreates a new, empty file at the specified path.
@param path The path where the file should be created.
createDirectory
createDirectory (path:string):voidCreates a new directory at the specified path.
@param path The path where the directory should be created.
copy
copy (source:string, target:string):voidCopies a file or directory from a source path to a target path.
@param source The source path.
@param target The target path.
move
move (source:string, target:string):voidMoves or renames a file or directory.
@param source The source path.
@param target The target path.
deleteFile
deleteFile (path:string):voidDeletes the file at the specified path.
@param path The path to the file to delete.
deleteDirectory
deleteDirectory (path:string, forced?:boolean):voidDeletes the directory at the specified path.
@param path The path to the directory to delete.
@param forced If true, recursively deletes the directory and its contents.
createTempFile
createTempFile (prefix:string, suffix:string):stringCreates a new temporary file with the given prefix and suffix.
@param prefix The prefix string to be used in generating the file's name.
@param suffix The suffix string to be used in generating the file's name.
@returns The path of the created temporary file.
createTempDirectory
createTempDirectory (prefix:string):stringCreates a new temporary directory with the given prefix.
@param prefix The prefix string to be used in generating the directory's name.
@returns The path of the created temporary directory.
createInputStream
createInputStream (path:string):InputStreamCreates and returns a new {@link InputStream} for reading data from the file.
@param path The path to the file.
@returns A new InputStream instance.
createOutputStream
createOutputStream (path:string):OutputStreamCreates and returns a new {@link OutputStream} for writing data to the file.
@param path The path to the file.
@returns A new OutputStream instance.
traverse
traverse (path:string):FolderObject[]Traverses a directory and returns a structured {@link FolderObject} hierarchy.
@param path The path to the folder to traverse.
@returns The root FolderObject containing the file system tree structure.
list
list (path:string):string[]Lists the direct children (files and folders) of a directory, returning only their paths.
@param path The path to the directory.
@returns An array of string paths for the contents of the directory.
find
find (path:string, pattern:string):string[]Finds files and directories matching a specified glob pattern within a directory tree.
@param path The starting path for the search.
@param pattern The glob pattern to match (e.g., "*.js", "**.txt").
@returns An array of string paths that match the pattern.