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></br> Note:<br></br>
- If there are no schemes defined for the path parameter of FSAPI methods, it considers to have "file:" scheme for the path.<br></br>
- 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></br>
- The native layer of UXP FSAPI is based on libUV except UWP powered features, such as FilePicker and Drag&Drop on Win10 XD.<br></br>
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.
Returns: Promise<string|ArrayBuffer> - the contents of the file
stringanystringfunctionExample
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.
Returns: string | ArrayBuffer - the contents of the file
stringanystringExample
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.
Returns: Promise<number> - the length of contents written to the file
stringstring | ArrayBuffer | ArrayBufferViewanystringfunctionExample
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.
Returns: number - the length of contents written to the file
stringstring | ArrayBuffer | ArrayBufferViewanystringExample
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
Returns: Promise<number> - fd(file descriptor)
stringfunctionExample
const fd = await fs.open("plugin-data:/fileToRead.txt", "r");
close(fd, callback)
Closes a file descriptor asynchronously
Returns: number - 0 if succeeded, otherwise throws an Error
numberfunctionExample
await fs.close(fd);
read(fd, buffer, offset, length, position, callback)
Reads data in chunks from the file it refers to the file descriptor
Returns: Promise<Object> - { bytesRead: number, buffer: ArrayBuffer }
Throws:
Errorif invalid file descriptor is passed. if invalid parameter format or value is passed.
numberArrayBuffernumbernumbernumberfunctionExample
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
numberArrayBuffernumbernumbernumberfunctionExample
const fd = await fs.open("plugin-data:/fileToWrite.txt", "w+");
const data = "It was a dark and stormy night.\n";
const srcBuffer = new Uint8Array(data.length);
for (let i = 0; i < data.length; i++) {
srcBuffer[i] = data.charCodeAt(i);
}
const { bytesWritten } = await fs.write(fd, srcBuffer.buffer, 0, srcBuffer.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<number> - 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<number> - 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<number> - 0 if succeeded, otherwise throws an Error
stringfunctionExample
await fs.unlink("plugin-data:/fileToDelete.txt");
mkdir(path, options, callback)
Creates a directory of the path asynchronously
Returns: Promise<number> - 0 if succeeded, otherwise throws an Error
string*booleanfalsefunctionExample
await fs.mkdir("plugin-data:/newDir");
rmdir(path, callback)
Removes a directory asynchronously
Returns: Promise<number> - 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");