fs file system
-
fs (file system) file system, which belongs to the core module of node.js, is used to read and write files
-
To use fs, you need to introduce this module first
-
The ID of the core module is the module name. Here is fs
const fs = require('fs');
Synchronous and asynchronous calls
- There are two types of operations in fs module: synchronous and asynchronous
- Synchronizing the file system may block program execution
- Asynchronous file systems do not block program execution. Asynchronous execution will return the result through the callback function after the operation is completed
File write
Synchronous file writing
① Open the file: fs.openSync(path[, flags[, mode]])
- Path: file path
- Flags: file system flags
- 'r': read data. If the file does not exist, an error is reported (default)
- 'w': write data. If the file does not exist, the file is created; Otherwise, the original contents of the file will be overwritten
- 'a': append data. If the file does not exist, the file is created; Otherwise, add content
- Mode: sets the file mode if the file has been created. It defaults to 0o666 and is generally not filled in
- Return value: an integer representing the file descriptor
const fs = require('fs'); let fd = fs.openSync('hello.txt', 'w'); console.log(fd); // 3
② Write content: fs.writeSync(fd, data[, position[, encoding]])
- fd: an integer representing the file descriptor
- data: contents to be written
- Position: the position to start writing (eg: 2 → empty two bits and then start writing data). Generally, it is not written
- Encoding: the encoding format of the written data. The default is utf8. Generally, it is not written
- Return value: number of bytes written
- If data is a normal object, it must have its own (not inherited) toString method property
const fs = require('fs'); let fd = fs.openSync('hello.txt', 'w'); let len = fs.writeSync(fd, 'hello superman'); console.log(len); // 14
③ Close file: fs.closeSync(fd)
- fd: an integer representing the file descriptor
const fs = require('fs'); let fd = fs.openSync('hello.txt', 'w'); fs.writeSync(fd, 'hello superman'); fs.closeSync(fd);
After writing, you need to close the file to avoid waste of resources
Asynchronous file writing
- The asynchronous method has no return value, and the results are obtained through the parameters of the callback function
① Open file: fs.open(path[, flags[, mode]], callback)
- Path: the path to the file
- Flags: file system flags
- 'r': read data. If the file does not exist, an error is reported (default)
- 'w': write data. If the file does not exist, the file is created; Otherwise, the original contents of the file will be overwritten
- 'a': append data. If the file does not exist, the file is created; Otherwise, add content
- Mode: sets the file mode if the file has been created. It defaults to 0o666 and is generally not filled in
- callBack: callBack function, after opening the file, it receives 2 parameters:
- err: error message, null by default
- fd: an integer representing the file descriptor
const fs = require('fs'); fs.open('hello.txt', 'w', function () { console.log(arguments); // [Arguments] { '0': null, '1': 3 } });
② Write data: fs.write(fd, string[, position[, encoding]], callback)
- fd: an integer representing the file descriptor
- string: content to be written
- Position: the position to start writing (eg: 2 → empty two bits and then start writing data). Generally, it is not written
- Encoding: the encoding format of the written data. The default is utf8. Generally, it is not written
- callBack: callBack function, receiving 3 parameters:
- err: error message, null by default
- len: number of bytes written
- Data: written data content
const fs = require('fs'); fs.open('hello.txt', 'w', function (err, fd) { // Open file fs.write(fd, 'Content written asynchronously', function () { // Write data console.log(arguments); // [arguments] {0 ': null,' 1 ': 21,' 2 ': asynchronously written content'} }); });
③ Close file: fs.close(fd, callBack)
- fd: an integer representing the file descriptor
- callBack: a callBack function that receives an exception parameter err, which is null by default
const fs = require('fs'); fs.open('hello.txt', 'w', function (err, fd) { // Open file if (!err) { console.log('open'); fs.write(fd, 'Content written asynchronously', function (err) { // Write data if (!err) { console.log('write'); } fs.close(fd, function (err) { // Close file if (!err) { console.log('close'); } }); }); } });
The program will first go to the code executed synchronously, and then to the code executed asynchronously
const fs = require('fs'); fs.open('hello.txt', 'w', function (err, fd) { // Open file if (!err) { console.log('open'); fs.write(fd, 'Content written asynchronously', function (err) { // Write data if (!err) { console.log('write'); } fs.close(fd, function (err) { // Close file if (!err) { console.log('close'); } }); }); } }); console.log('Synchronization program'); // Synchronization program
Output sequence: synchronizer → open file → write data → close file
Simple file write & read
Simple file write
fs.writeFileSync(path, data[, options]): return undefined
fs.writeFile(path, data[, options], callBack)
- Path: file path
- Data: data to be written
- options: sets the write operation, which is generally not written
- Encoding: the encoding format of data. The default is utf8
- Mode: sets the file mode if the file has been created. The default value is 0o666. Generally, it is not written
- Flag: file system flag. The default value is' w '
- callBack: callBack function, called after completion. Receive 1 Parameter err: error message, null by default
demo 1: writeFile() - create a fsWrite.txt file and write its contents
const fs = require('fs'); let content = 'Data written'; fs.writeFile('./fsWrite.txt', content, function (err) { console.log(err); // null });
demo 2: writeFile() - append content to fsWrite.txt file
const fs = require('fs'); let content = 'Additional content'; fs.writeFile('./fsWrite.txt', content, { flag: 'a' // The flag in option is set to a }, function (err) { console.log(err); // null });
Simple file read
fs.readFileSync(path[, options]): synchronous operation, which obtains the read data through the return value
fs.readFile(path[, options], callback): asynchronous operation to obtain read data through callback function
- Path: file path
- options: sets the write operation, which is generally not written
- Encoding: the encoding format of data. It is null by default
- Flag: file system flag. The default value is' r '
- Callback: callback function, receiving 2 parameters:
- err: error message, null by default
- Data: read data
demo 2: readFile() - read the contents of the fsWrite.txt file
const fs = require('fs'); fs.readFile('./fsWrite.txt', function (err, data) { if (!err) { console.log(data); // <Buffer 68 65 6c 6c 6f> console.log(data + ''); // hello } });
The read data is in Buffer format by default and can be converted into string form
Streaming file write & read
Streaming file write / read is suitable for large files
Streaming file write
① Create writable stream: fs.createWriteStream(path[, options])
- Path: file path
- options: sets the write operation, which is generally not written
- flags: file system flag. The default value is' w '
- Encoding: the encoding format of data. The default is utf8
- Return value: WriteStream object
const fs = require('fs'); let ws = fs.createWriteStream('./WS.txt');
② Write data: ws.write(data)
- Data: data to be written
const fs = require('fs'); let ws = fs.createWriteStream('./WS.txt'); let content = 'What needs to be written'; ws.write(content); ws.write(' Additional content');
③ You can listen to the flow's switches by listening to the flow's' open 'and' close 'events
Since both open and close operations occur only once, once() is used here to bind events
const fs = require('fs'); let ws = fs.createWriteStream('./WS.txt'); let content = 'What needs to be written'; ws.write(content); ws.write(' Additional content'); ws.once('open', function () { // Equivalent to one() in jQuery console.log('The writable stream is open'); }) ws.once('close', function () { console.log('The writable stream is closed'); })
④ Close writable stream: ws.close([callBack]) / ws.end([callBack])
- callBack: callBack function, which closes the writable stream and calls. The parameter err that receives an error message is null by default
const fs = require('fs'); let ws = fs.createWriteStream('./WS.txt'); let content = 'What needs to be written'; ws.write(content); ws.write(' Additional content'); ws.once('open', function () { // Equivalent to one() in jQuery console.log('The writable stream is open'); }) ws.close(function () { console.log('The writable stream is closed'); });
Streaming file reading
① Create readable stream: fs.createReadStream(path)
- Path: the path to the file
- Return value: ReadStream object
const fs = require('fs'); let rs = fs.createReadStream('./WS.txt');
② Bind a data event to the readable stream. After binding, it will automatically start reading data. Up to 65536 byte s can be read at a time
const fs = require('fs'); let rs = fs.createReadStream('./WS.txt'); rs.on('data', function (data) { console.log('Start reading data'); console.log(data); });
After reading, the readable stream will close automatically
③ You can listen to the flow's switches by listening to the flow's' open 'and' close 'events
Since both open and close operations occur only once, once() is used here to bind events
const fs = require('fs'); let rs = fs.createReadStream('./WS.txt'); rs.once('open', function () { console.log('readStream open!'); }); rs.once('close', function () { console.log('readStream close!'); }); rs.on('data', function (data) { console.log('Start reading data'); console.log(data); });
Used with writable streams
const fs = require('fs'); let rs = fs.createReadStream('./WS.txt'); let ws = fs.createWriteStream('./123.txt'); rs.once('open', function () { console.log('ReadStream open!'); }); rs.once('close', function () { console.log('ReadStream close!'); ws.end(); // After the readable stream is closed, the writable stream is closed }); ws.once('open', function () { console.log('WriteStream open!'); }); ws.once('close', function () { console.log('WriteStream close!'); }); rs.on('data', function (data) { console.log('Start reading data'); ws.write(data); // Write read data });
- pipe(): the data in the readable stream can be transferred directly to the writable stream
const fs = require('fs'); let rs = fs.createReadStream('./WS.txt'); let ws = fs.createWriteStream('./123.txt'); rs.once('open', function () { console.log('ReadStream open!'); }); rs.once('close', function () { console.log('ReadStream close!'); ws.end(); // After the readable stream is closed, the writable stream is closed }); ws.once('open', function () { console.log('WriteStream open!'); }); ws.once('close', function () { console.log('WriteStream close!'); }); rs.pipe(ws); // Write read data
Other operations
fs.statSync(path[, options]): a synchronization operation that returns the file status Stats object
fs.stat(path[, options], callback): asynchronous operation to obtain the file status Stats object through the callback function
- Path: the path to the file
- options: settings for get operations
- Bigint: whether the value in status is bigint. The default value is false
- callBack: receive 2 parameters:
- err: error message, null by default
- Status: status object, which holds information about the status of the current object
const fs = require('fs'); fs.stat('./WS.txt', function (err, status) { if (!err) { console.log(status); } });
fs.unlinkSync(path): return undefined
fs.unlink(path, callback): delete files
- Path: the path to the file
- callBack: parameter err that receives 1 error message. It is null by default
const fs = require('fs'); fs.unlink('./WS.txt', function (err) { console.log(err); });
fs.existsSync(path): check whether the file exists
- Path: the path to the file
- Returns true if the file exists; Otherwise, false is returned
const fs = require('fs'); console.log(fs.existsSync('./WS.txt'));
fs.readdirSync(path[, options]): returns directory information
fs.readdir(path[, options], callback): obtain directory information through the parameters of the callback function
- Path: the path to the file
- options: settings for read operations, generally not written
- Encoding: the encoding format of data. The default is utf8
- Callback: callback function, receiving 2 parameters:
- err: error message, null by default
- files: an array of file names in the directory
const fs = require('fs'); fs.readdir('.', function (err, files) { // View current directory if (!err) { console.log(files); } });
fs.truncateSync(path[, len]): return undefined
fs.truncate(path[, len], callback): intercept file contents
- Path: the path to the file
- len: truncates the file data to the specified size (byte), which is 0 by default
- Callback: callback function, which receives an error message parameter err, which is null by default
const fs = require('fs'); fs.truncateSync('./hello.txt', 5);
- Note that a Chinese character accounts for 2 / 3 bytes, so garbled code may appear after intercepting the specified byte
fs.mkdirSync(path[, options]): return undefined
fs.mkdir(path[, options], callback): create directory
- Path: the path of the folder
- options: settings for creating operations, which are generally not written
- recursive: whether it can be created recursively. The default value is false
- Callback: callback function, which receives an error message parameter err, which is null by default
const fs = require('fs'); fs.mkdir('mkdir', function (err) { console.log(err); })
fs.rmdirSync(path[, options]): return undefined
fs.rmdir(path[, options], callback): delete folder
- Path: the path of the folder
- options: settings for creating operations, which are generally not written
- Callback: callback function, which receives an error message parameter err, which is null by default
const fs = require('fs'); fs.rmdirSync('mkdir');
fs.renameSync(oldPath, newPath): return undefined
fs.rename(oldPath, newPath, callback): File renaming
- oldPath: the original path of the file
- newPath: the new path to the file
- Callback: callback function, which receives an error message parameter err, which is null by default
const fs = require('fs'); fs.renameSync('./hello.txt', './h.txt');
Because the parameter writes the path, you can also change the storage path of the file while renaming
const fs = require('fs'); fs.renameSync('./123.txt', '../123.txt');
If newPath already exists, it will be overwritten. If there is a directory in newPath, an error is raised
const fs = require('fs'); fs.renameSync('./h.txt', './123.txt');
fs.watchFile(filename[, options], listener): listen for file changes
- filename: file path
- Options: configuration options
- bigint: false by default
- persistent: the default value is true
- Interval: the listening interval. The default value is 5007
- listener: callback function, the monitored files are called after modification, and receive 2 parameters:
- Current: the current status of the file, which is the fs.status object
- previous: the status of the file before modification. It is also a fs.status object
- Return value: fs.StatWatcher object, which stores some listening information
const fs = require('fs'); let wf = fs.watchFile('fsWrite.txt', function (current, previous) { console.log('Current file size' + current.size); console.log('File previous size' + previous.size); }); console.log(wf);