This article introduces the NodeJS native file processing module fs. The program examples in this article are all from the reference documents at the end of the article.
fs module
Basically all file-related operations in NodeJS are done by the fs module, and the fs module import method
const fs = require('fs');
Functions of the fs module
The fs module provides the following functional modules:
-
file read
-
file write
-
file append write
-
file copy
-
Create a directory
Methods of the fs module
fs provides some file operation methods, each operation provides two methods: synchronous and asynchronous, and a Sync suffix is added after the synchronous method.
readFileSync/readFile
The readFile method is used to read a file and is called as follows
const fs = require("fs"); //Read files synchronously let buf = fs.readFileSync("1.txt");
let data = fs.readFileSync("1.txt", "utf8");
console. log(buf); // <Buffer 48 65 6c 6c 6f> console.log(data); // Hello //call asynchronously fs.readFile("1.txt", "utf8", (err, data) => {
if(!err){
console.log(data); // Hello
}
});
-
The first parameter is the path or file descriptor of the read file
-
The second parameter is options, the default value is null, including encoding (encoding, default is null) and flag (identification bit, default is r), you can also directly pass in encoding
-
The third parameter of the asynchronous method is the callback function.
writeFileSync/writeFile
The writeFile method is used to write a file, an example of the calling method
const fs = require("fs"); //Call fs.writeFileSync("2.txt", "Hello world") synchronously;
let data = fs.readFileSync("2.txt", "utf8");
console. log(data); // Hello world // asynchronously call fs.writeFile("2.txt", "Hello world", err => {
if (!err) {
fs.readFile("2.txt", "utf8 ", (err, data) => {
console. log(data); // Hello world
});
}
});
-
The first parameter is the path or file descriptor to write to the file
-
The second parameter is the data to write, the type is String or Buffer
-
The third parameter is options, the default value is null, which includes encoding (encoding, default is utf8), flag (identity bit, default is a) and mode (permission bit, default is 0o666), you can also directly pass in encoding.
-
The asynchronous method also has one more parameter, which is the callback function
appendFileSync/appendFile
appendFileSync/appendFile is used to append to write files
const fs = require("fs"); //synchronously call fs.appendFileSync("3.txt", " world");
let data = fs.readFileSync("3.txt", "utf8"); //asynchronous call fs.appendFile("3.txt", " world", err => {
if (!err) {
fs.readFile("3.txt", "utf8", (err, data) => {
console.log (data); // Hello world
});
}
});
-
The first parameter is the path or file descriptor to write to the file
-
The second parameter is the data to write, the type is String or Buffer
-
The third parameter is options, the default value is null, including encoding (encoding, default is utf8), flag (identity bit, default is a) and mode (permission bit, default is 0o666), you can also directly pass in encoding
-
Asynchronous call method has one more parameter callback function
copyFileSync/copyFile
copyFileSync/copyFile is used to copy files
const fs = require("fs"); //Call synchronously fs.copyFileSync("3.txt", "4.txt");
let data = fs.readFileSync("4.txt", "utf8");
console .log(data); // Hello world // asynchronous call const fs = require("fs");
fs.copyFile("3.txt", "4.txt", () => {
fs.readFile(" 4.txt", "utf8", (err, data) => {
console.log(data); // Hello world
});
});The first parameter is the source file path, the second parameter is the target file, and the asynchronous method also has one more parameter for the callback function
mkdirSync/mkdir
mkdirSync/mkdir for creating directories
//Synchronous call fs.mkdirSync("a/b/c") //Asynchronous call fs.mkdir("a/b/c", err => {
if (!err) console.log("Created successfully") ;
});Receives one parameter as the file path, and the asynchronous method also has one more parameter callback function.
Reference documentation
-
Interviewer: Tell me about your understanding of the fs module in Node? What are the common methods?