common method
fs.access()
: Check if the file exists and Node.js has permission to access it.fs.appendFile()
: Append data to the file. If the file does not exist, the file is created.fs.chmod()
: Change the permissions of the file (specified by the passed in filename). Related methods:fs.lchmod()
,fs.fchmod()
.fs.chown()
: Change the owner and group of a file (specified by the passed in filename). Related methods:fs.fchown()
,fs.lchown()
.fs.close()
: Close the file descriptor.fs.copyFile()
: Copy files.fs.createReadStream()
: Create a readable file stream.fs.createWriteStream()
: Create a writable file stream.fs.mkdir()
: new folder.fs.mkdtemp()
: Create a temporary directory.fs.rm()
: Delete files and directories can also be deleted.fs.rmdir()
: Delete folder.fs.rename()
: Rename/move a file or folder.fs.open()
: Set the file mode.fs.readdir()
: Read the contents of the directory.fs.readFile()
: Read the contents of the file. Related method: fs.read().fs.readlink()
: Read the value of a symbolic link.fs.realpath()
: Resolve relative file path pointers (…) to full paths.fs.stat()
: Returns the status of the file (specified by the passed file name), which can be used to determine whether the file exists. Related methods:fs.fstat()
,fs.lstat()
.fs.symlink()
: Create a symbolic link to the file.fs.truncate()
: Truncate the file identified by the passed filename to the specified length. Related methods:fs.ftruncate()
.fs.link()
: Create a new hard link pointing to the file.fs.unlink()
: Delete a file or symbolic link.fs.unwatchFile()
: Stop watching for changes on the file.fs.utimes()
: Change the timestamp of the file (specified by the passed in filename). Related methods:fs.futimes()
.fs.watchFile()
: Start watching for changes on the file. Related method: fs.watch().fs.writeFile()
: Write data to a file. Related method: fs.write().
Example of use
Check if a file/directory exists
usefs.access
// 判断文件/目录 是否存在
fs.access('./data', fs.constants.F_OK, err => {
console.log(`${
err ? '不存在' : '存在'}`);
})
// 判断文件是否可读
fs.access(file, constants.R_OK, (err) => {
console.log(`${
file} ${
err ? '不可读' : '可读'}`);
});
// 判断文件是否可写
fs.access(file, constants.W_OK, (err) => {
console.log(`${
file} ${
err ? '不可写' : '可写'}`);
});
usefs.stat()
fs.stat('./data', (err, stats) => {
if (err) console.log('文件/目录不存在')
// 不存在时,stats 为 undefined
console.log('stats:', stats)
})
Create a directory
Create a first-level folder, and multiple levels will report an error
fs.mkdir('./data', err => {
if (err) throw err
})
Create multi-level directories
fs.mkdir('./data/test/test', {
recursive: true }, err => {
if (err) throw err
})
delete directory
Delete the specified folder, and the folder must be empty
fs.rmdir('./data', err => {
if (err) throw err
})
Delete a directory, including all folders and files under the directory
fs.rmdir('./data/test', {
recursive: true }, err => {
if (err) throw err
})
Same effect as above
fs.rm('./data/test', {
recursive: true }, err => {
if (err) throw err
})
Delete Files
usefs.unlink
fs.unlink('./data/test.txt', err => {
if (err) throw err
})
usefs.rm
fs.rm('./data/test.txt', err => {
if (err) throw err
})