Module basic module

Module fs

File system processing module

Instructions:

1
var fs = require('fs');

Some notes:

  • Running fs.watch(filename) will return a watcher inherited from EventEmitter , which supports three events of'change','changeonly', and'renameonly'
  • fs.watchFile(target) and fs.unwatchFile(target) can still be used in pairs
  • fs.watchFile(target) will return a StatsWatcher object inherited from EventEmitter , calling fs.unwatchFile(target) equivalent to calling StatsWatcher.close() .

Static function

exists

Query whether the specified file or directory exists

1
static Boolean fs.exists(String path) async;

Call parameters:

  • path : String, specify the path to be queried

Return result:

  • Boolean , returns True to indicate that the file or directory exists

access

Query the user's authority to the specified file

1 2
static fs.access(String path, Integer mode = 0) async;

Call parameters:

  • path : String, specify the path to be queried
  • mode : Integer, specify the query permissions, the default is whether the file exists

Create hard link file, this method is not supported under windows

1 2
static fs.link(String oldPath, String newPath) async;

Call parameters:

  • oldPath : String, source file
  • newPath : String, the file to be created

Delete the specified file

1
static fs.unlink(String path) async;

Call parameters:

  • path : String, specify the path to be deleted

mkdir

Create a directory

1 2
static fs.mkdir(String path, Integer mode = 0777) async;

Call parameters:

  • path : String, specify the name of the directory to be created
  • mode : Integer, specify file permissions, Windows ignores this parameter

rmdir

Delete a directory

1
static fs.rmdir(String path) async;

Call parameters:

  • path : String, specify the name of the directory to be deleted

rename

Rename a file

1 2
static fs.rename(String from, String to) async;

Call parameters:

  • from : String, specify the renamed file
  • to : String, specify the new file name to be modified

copy

Copy a file

1 2
static fs.copy(String from, String to) async;

Call parameters:

  • from : String, specify the renamed file
  • to : String, specify the new file name to be modified

chmod

Set the access permission of the specified file, Windows does not support this method

1 2
static fs.chmod(String path, Integer mode) async;

Call parameters:

  • path : String, specify the operation file
  • mode : Integer, specify the set access authority

lchmod

Set the access authority of the specified file. If the file is a soft link, the authority to the file will not be changed. It is only available on macOS and BSD series platforms

1 2
static fs.lchmod(String path, Integer mode) async;

Call parameters:

  • path : String, specify the operation file
  • mode : Integer, specify the set access authority

chown

Set the owner of the specified file, Windows does not support this method

1 2 3
static fs.chown(String path, Integer uid, Integer gid) async;

Call parameters:

  • path : String, specify the set file
  • uid : Integer, user id of the file owner
  • gid : Integer, file owner group id

lchown

Set the owner of the specified file. If the specified file is a soft link, the owner of the pointed file will not be changed. Windows does not support this method

1 2 3
static fs.lchown(String path, Integer uid, Integer gid) async;

Call parameters:

  • path : String, specify the set file
  • uid : Integer, user id of the file owner
  • gid : Integer, file owner group id

stat

Query the basic information of the specified file

1
static Stat fs.stat(String path) async;

Call parameters:

  • path : String, specify the file to be queried

Return result:

  • Stat , returns the basic information of the file

lstat

Query the basic information of the specified file. Unlike stat, when the path is a soft connection, the information returned will be the information of the soft connection instead of the information of the pointed file

1
static Stat fs.lstat(String path) async;

Call parameters:

  • path : String, specify the file to be queried

Return result:

  • Stat , returns the basic information of the file

Read the specified soft connection file, this method is not supported under windows

1
static String fs.readlink(String path) async;

Call parameters:

  • path : String, specify the soft connection file to be read

Return result:

  • String , returns the file name pointed to by the soft link

realpath

Returns the absolute path of the specified path, if the specified path contains a relative path, it will also be expanded

1
static String fs.realpath(String path) async;

Call parameters:

  • path : String, specify the path to read

Return result:

  • String , returns the processed absolute path

Create soft connection file

1 2 3
static fs.symlink(String target, String linkpath, String type = "file") async;

Call parameters:

  • target : String, the target file, which can be a file, a directory, or a path that does not exist
  • linkpath : String, the soft link file to be created
  • type : String, the type of soft connection created, the optional types are'file','dir','junction', the default is'file', this parameter is only valid on windows, it will be created when it is'junction' The target path linkpath must be an absolute path, and the target will be automatically converted to an absolute path.

truncate

Modify the file size, if the specified length is greater than the size of the source file, fill it with'\0', otherwise the more file content will be lost

1 2
static fs.truncate(String path, Integer len) async;

Call parameters:

  • path : String, specify the path of the modified file
  • len : Integer, specify the size of the modified file

read

According to the file descriptor, read the contents of the file

1 2 3 4 5
static Integer fs.read(Integer fd, Buffer buffer, Integer offset = 0, Integer length = 0, Integer position = -1) async;

Call parameters:

  • fd : Integer, file descriptor
  • buffer : Buffer , the Buffer object to which the read result is written
  • offset : Integer, Buffer write offset, default is 0
  • length : Integer, the number of bytes read from the file, the default is 0
  • position : Integer, the file reading position, the default is the current file position

Return result:

  • Integer , the number of bytes actually read

fchmod

According to the file descriptor, the file mode is changed. Only valid in POSIX system.

1 2
static fs.fchmod(Integer fd, Integer mode) async;

Call parameters:

  • fd : Integer, file descriptor
  • mode : Integer, the mode of the file

fchown

According to the file descriptor, the owner is changed. Only valid in POSIX system.

1 2 3
static fs.fchown(Integer fd, Integer uid, Integer gid) async;

Call parameters:

  • fd : Integer, file descriptor
  • uid : Integer, user id
  • gid : Integer, group id

fdatasync

According to file descriptor, synchronize data to disk

1
static fs.fdatasync(Integer fd) async;

Call parameters:

  • fd : Integer, file descriptor

fsync

According to file descriptor, synchronize data to disk

1
static fs.fsync(Integer fd) async;

Call parameters:

  • fd : Integer, file descriptor

readdir

Read file information in the specified directory

1
static NArray fs.readdir(String path) async;

Call parameters:

  • path : String, specify the directory to be queried

Return result:

  • NArray , returns the file information array of the directory

openFile

Open file for reading, writing, or simultaneous reading and writing

1 2
static SeekableStream fs.openFile(String fname, String flags = "r") async;

Call parameters:

  • fname : String, specify the file name
  • flags : String, specify the file opening method, the default is "r", read-only

Return result:

The supported methods of parameter flags are as follows:

  • 'r' is read-only, an error will be thrown if the file does not exist.
  • 'r+' read and write mode, an error will be thrown if the file does not exist.
  • 'w' is written only, the file will be created automatically if it does not exist, and will be cleared if it exists.
  • 'w+' read and write mode, the file is automatically created if it does not exist.
  • 'a' only write add method, the file will be created automatically if it does not exist.
  • 'a+' read and write adding method, the file will be created automatically if it does not exist.

open

Open file descriptor

1 2 3
static Integer fs.open(String fname, String flags = "r", Integer mode = 0666) async;

Call parameters:

  • fname : String, specify the file name
  • flags : String, specify the file opening method, the default is "r", read-only
  • mode : Integer, when creating a file, specify the mode of the file, the default is 0666

Return result:

  • Integer , returns the open file descriptor

The supported methods of parameter flags are as follows:

  • 'r' is read-only, an error will be thrown if the file does not exist.
  • 'r+' read and write mode, an error will be thrown if the file does not exist.
  • 'w' is written only, the file will be created automatically if it does not exist, and will be cleared if it exists.
  • 'w+' read and write mode, the file is automatically created if it does not exist.
  • 'a' only write add method, the file will be created automatically if it does not exist.
  • 'a+' read and write adding method, the file will be created automatically if it does not exist.

close

Close file descriptor

1
static fs.close(Integer fd) async;

Call parameters:

  • fd : Integer, file descriptor

openTextStream

Open a text file for reading, writing, or simultaneous reading and writing

1 2
static BufferedStream fs.openTextStream(String fname, String flags = "r") async;

Call parameters:

  • fname : String, specify the file name
  • flags : String, specify the file opening method, the default is "r", read-only

Return result:

The supported methods of parameter flags are as follows:

  • 'r' is read-only, an error will be thrown if the file does not exist.
  • 'r+' read and write mode, an error will be thrown if the file does not exist.
  • 'w' is written only, the file will be created automatically if it does not exist, and will be cleared if it exists.
  • 'w+' read and write mode, the file is automatically created if it does not exist.
  • 'a' only write add method, the file will be created automatically if it does not exist.
  • 'a+' read and write adding method, the file will be created automatically if it does not exist.

readTextFile

Open the text file and read the content

1
static String fs.readTextFile(String fname) async;

Call parameters:

  • fname : String, specify the file name

Return result:

  • String , returns the text content of the file

readFile

Open the binary file and read the content

1 2
static Variant fs.readFile(String fname, String encoding = "") async;

Call parameters:

  • fname : String, specify the file name
  • encoding : String, specify the decoding method, the default is not to decode

Return result:

  • Variant , returns the text content of the file

readLines

Open the file and read a group of text lines in an array. The end of the line is based on the setting of the EOL attribute. By default, posix: "\n"; windows: "\r\n"

1 2
static Array fs.readLines(String fname, Integer maxlines = -1);

Call parameters:

  • fname : String, specify the file name
  • maxlines : Integer, specify the maximum number of lines read this time, read all text lines by default

Return result:

  • Array , returns the read text line array, if there is no data to read, or the connection is interrupted, empty array

writeTextFile

Create a text file and write content

1 2
static fs.writeTextFile(String fname, String txt) async;

Call parameters:

  • fname : String, specify the file name
  • txt : String, specify the string to be written

writeFile

Create a binary file and write content

1 2
static fs.writeFile(String fname, Buffer data) async;

Call parameters:

  • fname : String, specify the file name
  • data : Buffer , specify the binary data to be written

appendFile

Create a binary file and write content

1 2
static fs.appendFile(String fname, Buffer data) async;

Call parameters:

  • fname : String, specify the file name
  • data : Buffer , specify the binary data to be written

setZipFS

Set zip virtual file mapping

1 2
static fs.setZipFS(String fname, Buffer data);

Call parameters:

  • fname : String, specify the mapping path
  • data : Buffer , specify the mapped zip file data

clearZipFS

Clear zip virtual file mapping

1
static fs.clearZipFS(String fname = "");

Call parameters:

  • fname : String, specify the mapping path, clear all caches by default

watch

Observe a file and return the corresponding watcher object

1
static FSWatcher fs.watch(String fname);

Call parameters:

  • fname : String, specify the file object to be observed

Return result:


Observe a file and return the corresponding watcher object

1 2
static FSWatcher fs.watch(String fname, Function callback);

Call parameters:

  • fname : String, specify the file object to be observed
  • callback : Function, (evtType: 'change' | 'rename', filename: string) => any Processing callback when the file object changes

Return result:


Observe a file and return the corresponding watcher object

1 2
static FSWatcher fs.watch(String fname, Object options);

Call parameters:

  • fname : String, specify the file object to be observed
  • options : Object, observation options
  • options.persistent : {boolean} default: true whether the process will not exit as long as the target file is still being observed
  • options.recursive : {boolean} default: false If fname is a folder, whether to observe all subdirectories under it recursively
  • options.encoding : {enum} default:'utf8' specifies the character encoding for parsing the incoming fname

Return result:


Observe a file and return the corresponding watcher object

1 2 3
static FSWatcher fs.watch(String fname, Object options, Function callback);

Call parameters:

  • fname : String, specify the file object to be observed
  • options : Object, observation options
  • options.persistent : {boolean} default: true whether the process will not exit as long as the target file is still being observed
  • options.recursive : {boolean} default: false If fname is a folder, whether to observe all subdirectories under it recursively
  • options.encoding : {enum} default:'utf8' specifies the character encoding for parsing the incoming fname
  • callback : (evtType: 'change' | 'rename', filename: string) => any Processing callback when the file object changes

Return result:


watchFile

Observe a file and return the corresponding StatsWatcher object

1 2
static StatsWatcher fs.watchFile(String fname, Function callback);

Call parameters:

  • fname : String, specify the file object to be observed
  • callback : Function, (curStats: Stats, prevStats: Stats) => any Processing callback when the stats of the file object changes

Return result:


Observe a file and return the corresponding StatsWatcher object

1 2 3
static StatsWatcher fs.watchFile(String fname, Object options, Function callback);

Call parameters:

  • fname : String, specify the file object to be observed
  • options : Object, observation options
  • options.persistent : {boolean} default: true whether the process will not exit as long as the target file is still being observed
  • options.recursive : {boolean} default: false If fname is a folder, whether to observe all subdirectories under it recursively
  • options.encoding : {enum} default:'utf8' specifies the character encoding for parsing the incoming fname
  • callback : (curStats: Stats, prevStats: Stats) => any Processing callback when the stats of the file object changes

Return result:


unwatchFile

Remove the callbacks of all observation events from the StatsWatcher that observes fname

1
static fs.unwatchFile(String fname);

Call parameters:

  • fname : String, specify the file object to be observed

Return result:


Remove the callback callback from the observation event callback of the StatsWatcher that observes fname

1 2
static fs.unwatchFile(String fname, Function callback);

Call parameters:

  • fname : String, specify the file object to be observed
  • callback : Function, callback to be removed

Return result:

Even if the callback is no longer in the StatsWatcher observation event callback, no error will be reported

Static properties

constants

Object, constant object of fs module

1
static readonly Object fs.constants;

constant

SEEK_SET

seek mode constant, move to absolute position

1
const fs.SEEK_SET = 0;

SEEK_CUR

The seek mode constant, move to the relative position of the current position

1
const fs.SEEK_CUR = 1;

SEEK_END

seek mode constant, move to the relative position of the end of the file

1
const fs.SEEK_END = 2;