The File class exposes functions which allow access to binary and text files.
// the file this example creates, in the system's temporary directory
// (Environment.getTempPath() does not include a trailing separator)
const path = `${Environment.getTempPath()}/data.bin`;
// create a binary file
let f = File.open(path,
FileMode.Create,
FileAccess.ReadWrite,
FileShare.None);
// make a buffer with some data to write
const buf = new MemoryBuffer(3);
buf[0] = 0;
buf[1] = 1;
buf[2] = 2;
// write the buffer to the file
f.write(buf, 0, 3);
// close the file
f.close();
// check to make sure it exists
if (File.exists(path)) {
alert("file created successfully.");
}
// open up the file for reading
f = File.open(path,
FileMode.Open,
FileAccess.Read,
FileShare.None);
buf.clear();
// read 3 bytes from the file
f.read(buf, 0, 3);
f.close();
alert(`The following value should be 2. It is: ${buf[2]}`); Returns a TextWriter object, or null if a problem occurred.
Opens a file specified by the filename parameter for appending. If the file does not already exist, it is created.
Returns true upon success, and false otherwise.
Copies the source file to a new destination file. Returns true if the new file was created and false otherwise.
Returns the method returns a TextWriter object, or null if a problem was encountered.
Creates a text file using the filename specified in the filename parameter. If the file already exists, it will be overwritten.
Returns true upon success, and false otherwise.
Deletes the file specified by filename and returns a boolean value indicating success or failure.
Returns true if the file exists, and false otherwise.
Determines whether the file specified by filename exists.
Returns fully-qualified path for a temporary file.
Generates a unique filename for a temporary file. The function will use the system's default temporary path. For Windows systems, the path will be the users configured temporary directory (usually in C:\Documents and Settings\(user name)\Local Settings\Temp). For Linux systems, this will usually be the /tmp directory.
Returns true upon success, and false otherwise.
Moves the file specified by file_path to the new_file_path and returns a boolean value indicating success or failure.
Returns a FileStream object, or null if an error was encountered.
Opens the file specified by filename, applying the functionality requested by the file_mode, file_access and file_share parameters.
Returns a TextReader object, or null if an error occurred.
Opens the text file specified by filename and returns a TextReader object.