This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging
Preface:
Node.js provides the FS file system module.
What is the FS filesystem module
Browsers are powerful, but they can’t handle files on a user’s computer, such as opening specific files, adding or deleting files from folders. The fs module is an official module provided by Node.js for manipulating files. It provides a series of methods and properties that can be used to meet the user’s operational needs for files.
For example, the fs.readfile () method is used to read the contents of the specified file. The fs.writefile () method is used to write to the specified file. If you want to use the FS module to manipulate the file in JavaScript code, you need to import it in the following way:
const fs = reuire('fs')
Copy the code
1. How do I read the contents of a specified file
fs.readFile()
fs.readFile(path[, options], callback)
Copy the code
Parameter 1: This parameter is mandatory. You need to specify a character string of the file path to read. Parameter 2: An optional parameter that indicates the encoding in which the file is read. Parameter 3: This parameter is mandatory. After the file is read, the callback function is used to obtain the read result.
Read the contents of the specified file in utf8 format and print err and data values:
const fs = require('fs'); Fs. readFile('hello.txt', 'utF-8 ', (err, data) => {// Check whether the data is read successfully if (err) return console.log(err); console.log(data); });Copy the code
2. How can I write content to a specified file
Fs.writefile () is used to write to a specified file. The syntax format is as follows:
fs.writeFile(file, data[, options], callback)
Copy the code
Parameter 1: This parameter is mandatory. You need to specify a file path. Parameter 2: Specifies the content to be written. This parameter is mandatory. Parameter 3: Specifies the format in which the file content is to be written. The default value is UTf8. Parameter 4: This parameter is mandatory. It is a callback function after the file is written.
The sample code
const fs = require('fs'); Fs. writeFile('./hello. TXT ', 'Hello node', (err) => {// Check whether the write is successful if (err) return console.log(err); Console. log(' write successful '); });Copy the code
3. Read the names of all files in the specified directory
Using the fs.readdir() method, you can read the names of all files in the specified directory. The syntax format is as follows:
fs.readdir(path[, options], callback)
Copy the code
Parameter 1: This parameter is mandatory. It indicates the name list of files in the directory to be read. Parameter 2: Optional parameter, the format in which the file name in the directory is read. The default value is UTf8. Parameter 3: This parameter is mandatory. It is a callback function after the read is complete.
The sample code
const fs = require('fs'); Fs. readdir('./', (err, data) => {// Error handling if (err) return console.log(err); console.log(data); });Copy the code
conclusion
How time flies! It’s the ninth day of August. Keep going. When sharing becomes fun to write, it shows you are growing