require('fs')
UXP Provides Node.js style file system API, FSAPI. Unlike Entry based File or Folder classes, these methods can directly access a local file or folder with path or file descriptor. The starting point of a path in the native filesystem depends on the scheme. UXP supports plugin-specific storage schemes, such as "plugin:", "plugin-data:", and "plugin-temp:", as well as a native "file:" scheme for the path parameter.<br/> Note1: UWP(Universal Windows Platform) has the strict File access permissions, and UXP FSAPI may have access issues with anonymous filepaths. So, XD does not support this feature for compatibility across platforms.<br/> Note2: The native layer of UXP FSAPI is based on libUV except UWP powered features, such as FilePicker and Drag&Drop on Win10 XD.
readFile(path, options, callback)
Reads data from the path asynchronously. The file format can be specified with the encoding options. If an encoding is not supplied, the file is assumed to be a binary format.
stringanystringfunctionReturns: Promise<(string|ArrayBuffer)> - the contents of the file
Example
const data = await fs.readFile("plugin-data:/binaryFile.obj");
Example
const text = await fs.readFile("plugin-data:/textFile.txt", {encoding: "utf-8"});
readFileSync(path, options)
Reads data from the path synchronously. The file format can be specified with the encoding options. If an encoding is not supplied, the file is assumed to be a binary format.
stringanystringReturns: string | ArrayBuffer - the contents of the file
Example
const data = fs.readFileSync("plugin-data:/binaryFile.obj");
Example
const text = fs.readFileSync("plugin-data:/textFile.txt", {encoding: "utf-8"});
writeFile(path, data, options, callback)
Writes data to the path asynchronously, appending if desired. The format of the file is controlled via the encoding option, and defaults to a binary format.
stringstring | ArrayBuffer | ArrayBufferViewanystringfunctionReturns: Promise<int> - the length of contents written to the file
Example
const bufLen = await fs.writeFile("plugin-data:/binaryFile.obj", new Uint8Array([1, 2, 3]));
Example
const strLen = await fs.writeFile("plugin-data:/textFile.txt", "It was a dark and stormy night.\n", {encoding: "utf-8"});
writeFileSync(path, data, options)
Writes data to a path synchronously, appending if desired. The format of the file is controlled via the encoding option, and defaults to a binary format.
stringstring | ArrayBuffer | ArrayBufferViewanystringReturns: int - the length of contents written to the file
Example
const bufLen = fs.writeFileSync("plugin-data:/binaryFile.obj", new Uint8Array([1, 2, 3]));
Example
const strLen = fs.writeFileSync("plugin-data:/textFile.txt", "It was a dark and stormy night.\n", {encoding: "utf-8"});
open(path, [flag], [mode], callback)
Opens or a creates a file asynchronously
stringfunctionReturns: Promise<int> - fd(file descriptor)
Example
const fd = await fs.open("plugin-data:/fileToRead.txt", "r");
close(fd, callback)
Closes a file descriptor asynchronously
intfunctionReturns: int - 0 if succeeded, otherwise throws an Error
Example
await fs.close(fd);
read(fd, buffer, offset, length, position, callback)
Reads data in chunks from the file it refers to the file descriptor
Throws:
Errorif invalid file descriptor is passed. if invalid parameter format or value is passed.
intArrayBufferintintintfunctionReturns: Promise<Object> - { bytesRead, buffer }
Example
const fileSize = 1024;
const buffer = new ArrayBuffer(fileSize);
const fd = await fs.open("plugin-data:/fileToRead.txt", "r");
let bytesReadInTotal = 0;
while (bytesReadInTotal < fileSize) {
const { bytesRead } = await fs.read(fd, buffer, bytesReadInTotal, 128, -1);
if (!bytesRead) {
break;
}
bytesReadInTotal += bytesRead;
}
await fs.close(fd);
write(fd, buffer, offset, length, position, callback)
Writes data in chunks to the file it refers to the file descriptor
Returns: Promise<Object> - { bytesWritten, buffer }
Throws:
Errorif invalid file descriptor is passed if invalid parameter format or value is passed
intArrayBufferintintintfunctionExample
const fd = await fs.open("plugin-data:/fileToWrite.txt", "w+");
const data = "It was a dark and stormy night.\n";
const srcBuffer = new TextEncoder().encode(data).buffer;
const { bytesWritten } = await fs.write(fd, srcBuffer, 0, data.length, 0);
await fs.close(fd);
lstat(path, callback)
Gets information asynchronously from a file or a folder of the path
Returns: Promise<Object> - see Stats class in Node.js Note: Some methods or properties may not be supportive for the return object due to the platform limitation
stringfunctionExample
const stats = await fs.lstat("plugin-data:/textFile.txt");
const isFile = stats.isFile();
lstatSync(path)
Gets information synchronously from a file or a folder of the path
Returns: Object - see Stats class in Node.js Note: Some methods or properties may not be supportive for the return object due to the platform limitation
stringExample
const stats = fs.lstatSync("plugin-data:/textFile.txt");
const birthTime = stats.birthtime;
rename(oldPath, newPath, callback)
Renames or moves, if required, the file from the oldPath to the newPath
Returns: Promise<int> - 0 if succeeded, otherwise throws an Error
stringstringfunctionExample
fs.rename("plugin-data:/oldName.txt", "plugin-temp:/newName.txt");
copyFile(srcPath, destPath, flags, callback)
Copies a file or a folder from the source path to the destination path
Returns: Promise<int> - 0 if succeeded, otherwise throws an Error
stringstringfunctionExample
const data = fs.copyFile("plugin-data:/copyFrom.txt", "plugin-temp:/copyTo.txt");
unlink(path, callback)
Deletes a name with the file it refers to asynchronously
Returns: Promise<int> - 0 if succeeded, otherwise throws an Error
stringfunctionExample
await fs.unlink("plugin-data:/fileToDelete.txt");
mkdir(path, callback)
Creates a directory of the path asynchronously
Returns: Promise<int> - 0 if succeeded, otherwise throws an Error
stringfunctionExample
await fs.mkdir("plugin-data:/newDir");
rmdir(path, callback)
Removes a directory asynchronously
Returns: Promise<int> - 0 if succeeded, otherwise throws an Error
stringfunctionExample
await fs.rmdir("plugin-data:/dirToRemove");
readdir(path, callback)
Reads a directory to list the containing files and directories asynchronously
Returns: Promise<Array<string>> - string array of containing files and directories in the path
stringfunctionExample
const paths = await fs.readdir("plugin-data:/dirToRead");
readdirSync(path)
Reads a directory to list the containing files and directories synchronously
Returns: Array.<string> - string array of containing files and directories in the path
stringExample
const paths = fs.readdirSync("plugin-data:/dirToRead");