File
Represents a file on a file system. Provides methods for reading from and writing to the file. You'll never instantiate a File directly; instead you'll get access via a storage.FileSystemProvider. Keep in mind that File as such doesn't need a require() statement, however a localFileSystem will need it.
Example
// Get the object of a File instance
const fs = require('uxp').storage.localFileSystem;
const file = await fs.createEntryWithUrl("file:/Users/user/Documents/tmp"); // Gets a File instance
console.log(file.isFile); // returns true
isFile
Indicates that this instance is a file.
Example
if (anEntry.isFile) {
await anEntry.read();
}
mode : Symbol
Indicates whether this file is read-only or read-write. See readOnly and readWrite.
Example
if (aFile.mode === modes.readOnly) {
throw new Error("Can't write to a file opened as read-only.");
}
read(options)
Reads data from the file and returns it. The file format can be specified with the format option. If a format is not supplied, the file is assumed to be a text file using UTF8 encoding.
Returns: Promise<string|ArrayBuffer> the contents of the file
anyExample
const text = await myNovel.read();
Example
const data = await myNovel.read({format: formats.binary});
write(data, options)
Writes data to a file, appending if desired. The format of the file is controlled via the format option, and defaults to UTF8.
Returns: Promise<number> - the length of the contents written to the file
Throws:
FileIsReadOnlyif writing to a read-only fileOutOfSpaceIf writing to the file causes the file system to exceed the available space (or quota)
string | ArrayBufferanybooleanfalsetrue, the data is written to the end of the fileExample
await myNovel.write("It was a dark and stormy night.\n");
await myNovel.write("Cliches and tropes aside, it really was.", {append: true});
Example
const data = new ArrayBuffer();
await aDataFile.write(data, {format: formats.binary});
isFile(entry)
Determines if the entry is a file or not. This is safe to use even if the entry is null or undefined.
Returns: boolean - if true, the entry is a file.
any