Module basic module

Module fs

File system processing module

Instructions:

1
var fs = require('fs');

Some points to note:

  • Run fs.watch(filename)returns from an inheritanceEventEmitter Watcher, it supports'change','changeonly','renameonly' three events
  • fs.watchFile(target)And fs.unwatchFile(target)can still be used in pairs
  • fs.watchFile(target) Will return an inherited from EventEmitter of StatsWatcherObject, call fs.unwatchFile(target)equivalent to calling StatsWatcher.close().
  • Since uv on the Linux implementation, fs.watchthe recursiveoptions win32 / darwin is stable support. You can still try to try to use only in Linux fs.watch('/[path](path.md)/to', { recursive: true }, handler), but may find handlerthe callback time differ from your expectations

Object

constants

Constant object of fs module

1
fs_constants fs.constants;

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, default value: 0777

Create a directory

1 2
static fs.mkdir(String path, Object opt) async;

Call parameters:

  • path: String, specify the name of the directory to be created
  • opt: Object, specify creation parameters

The creation parameter can contain the following values:

1 2 3 4
{ recursive: false, // 指定是否父目录不存在是是否自动创建,默认值: false mode: 0777 // 指定文件权限,Windows 忽略此参数,默认值: 0777 }

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

copyFile

Copy src to dest. By default, if dest already exists, it will be overwritten.

1 2 3
static fs.copyFile(String from, String to, Integer mode = 0) async;

Call parameters:

  • from: String, specify the name of the source file to be copied
  • to: String, specify the name of the target file to be copied
  • mode: Integer, specify the modifier of the copy operation, the default is 0

mode is an optional integer that specifies the behavior of the copy operation. You can create a mask consisting of the bitwise OR of two or more values ​​(such asfs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE).

  • fs.constants.COPYFILE_EXCL-If dest already exists, the copy operation will fail.
  • fs.constants.COPYFILE_FICLONE-The copy operation will attempt to create a copy-on-write link. If the platform does not support copy-on-write, a backup copy mechanism is used.
  • fs.constants.COPYFILE_FICLONE_FORCE-The copy operation will attempt to create a copy-on-write link. If the platform does not support copy-on-write, the copy operation will fail.

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 permission of the specified file. If the file is a soft link, the permission 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, the file of the specified settings
  • uid: Integer, file owner user id
  • 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, the file of the specified settings
  • uid: Integer, file owner user id
  • 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, Return the basic information of the file

lstat

Query the basic information of the specified file. The difference from stat is that whenpathWhen it is a soft link, the information of the soft link will be returned 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, Return 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, Return 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, Return 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, when it is'junction', the target will be created The 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, Read the result written Buffer Object
  • 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 systems.

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

Call parameters:

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

fchown

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

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 the file information of 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 the file for reading, writing, or reading and writing at the same time

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. If the file does not exist, it will be created automatically, and if it exists, it will be cleared.
  • 'w+' read and write mode, the file will be created automatically if it does not exist.
  • 'a' only write the adding 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, Return 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. If the file does not exist, it will be created automatically, and if it exists, it will be cleared.
  • 'w+' read and write mode, the file will be created automatically if it does not exist.
  • 'a' only write the adding 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 reading and writing at the same time

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. If the file does not exist, it will be created automatically, and if it exists, it will be cleared.
  • 'w+' read and write mode, the file will be created automatically if it does not exist.
  • 'a' only write the adding 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 identified 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 up 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) => anythe 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

Return result:

The supported options are as follows:

1 2 3 4 5
{ "persistent": true, // {boolean} default: true 是否只要目标文件还在被观察, 进程就不退出 "recursive": false, // {boolean} default: false 对于 fname 为文件夹的情况, 是否递归地观察其下所有的子目录 "encoding": "utf8", // {enum} default: 'utf8' 指定解析传入的 fname 的字符编码 }

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
  • callback: Function, (evtType: 'change' | 'rename', filename: string) => anythe processing callback when the file object changes

Return result:

The supported options are as follows:

1 2 3 4 5
{ "persistent": true, // {boolean} default: true 是否只要目标文件还在被观察, 进程就不退出 "recursive": false, // {boolean} default: false 对于 fname 为文件夹的情况, 是否递归地观察其下所有的子目录 "encoding": "utf8", // {enum} default: 'utf8' 指定解析传入的 fname 的字符编码 }

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) => anythe 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
  • callback: Function, (curStats: Stats, prevStats: Stats) => anythe processing callback when the stats of the file object changes

Return result:

The supported options are as follows:

1 2 3 4 5
{ "persistent": true, // {boolean} default: true 是否只要目标文件还在被观察, 进程就不退出 "recursive": false, // {boolean} default: false 对于 fname 为文件夹的情况, 是否递归地观察其下所有的子目录 "encoding": "utf8", // {enum} default: 'utf8' 指定解析传入的 fname 的字符编码 }

unwatchFile

From the observation of fname StatsWatcher Remove all observed events in the callback

1
static fs.unwatchFile(String fname);

Call parameters:

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

Return result:


From the observation of fname StatsWatcherThe observed event callback removed callbackcallback

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 StatsWatcher No error will be reported in the observation event callback

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;