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
Copied to your clipboard1// Get the object of a File instance2const fs = require('uxp').storage.localFileSystem;3const file = await fs.createEntryWithUrl("file:/Users/user/Documents/tmp"); // Gets a File instance4console.log(file.isFile); // returns true
isFile
Indicates that this instance is a file.
Example
Copied to your clipboard1if (anEntry.isFile) {2 await anEntry.read();3}
mode : Symbol
Indicates whether this file is read-only or read-write. See readOnly and readWrite.
Example
Copied to your clipboard1if (aFile.mode === modes.readOnly) {2 throw new Error("Can't write to a file opened as read-only.");3}
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
Param | Type | Default | Description |
---|---|---|---|
options | any | ||
[options.format] | Symbol | formats.utf8 |
Example
Copied to your clipboardconst text = await myNovel.read();
Example
Copied to your clipboardconst 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:
FileIsReadOnly
if writing to a read-only fileOutOfSpace
If writing to the file causes the file system to exceed the available space (or quota)
Param | Type | Default | Description |
---|---|---|---|
data | string | ArrayBuffer | the data to write to the file | |
options | any | ||
[options.format] | Symbol | formats.utf8 | |
[options.append] | boolean | false | if true , the data is written to the end of the file |
Example
Copied to your clipboard1await myNovel.write("It was a dark and stormy night.\n");2await myNovel.write("Cliches and tropes aside, it really was.", {append: true});
Example
Copied to your clipboard1const data = new ArrayBuffer();2await 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.
Param | Type | Description |
---|---|---|
entry | any | the entry to check |