fs
(short for File System), which gives the developer the means to work with the file system. You can import it into the project as follows:
var fs = require('fs');
function fileHandler(){ fs.readFile('textFile.txt', 'utf8', (err, data) => { if(err) throw err; console.log(data); }); }
.readFile()
method, which we'll talk about .readFile()
, is intended for reading files. In this example, the callback function has two parameters, err
and data
. The first parameter contains errors that may occur when trying to read a file, the second is the data obtained after the successful execution of the operation. Note that .readFile()
is an asynchronous method of the fs
module. Its synchronous version is called .readFileSync()
. A similar approach is used to name other methods of the module.
function fileHandler(){ fs.open('testFile.txt', 'w', (err) => { if(err) throw err; console.log('File created'); }); }
fs.open()
method is used to create a new file. It takes the file name as the first argument. Its second argument is a flag indicating to the system what exactly we want to do with the file. In this case, it is the w
flag (short for writing), which indicates that we want to open the file for writing. The .open()
method can accept various flags. Here are some of them:
r
: open file for readingr+
: open file for reading and writingrs
: open file for reading in synchronous modew
: open file for writinga
: open file to write data to the end of filea+
: open file for reading and writing data to the end of filetestFile.txt
edit the newly created testFile.txt
file with a text editor. Fill in the following text:
This is a test file. We're learning about Node.js File System. The End.
function fileHandler(){ fs.appendFile('testFile.txt', ' This line is beyond the end.', (err) => { if(err) throw err; console.log('Data has been added!'); }); }
.appendFile()
method to add data to the end of an existing file. As the first argument, this method takes the name of the file, as the second - the data that needs to be added to the end of the file. The third argument is, as usual, the callback function.
This is a test file. We're learning about Node.js File System. The End. This line is beyond the end.
.writeFile()
method. This method is very similar to .appendFile()
, but it has one important difference. The fact is that using the .appendFile()
method, we add new data to the file after the data that it already has. And when using the .writeFile()
method, the contents of the file are replaced with a new one. Test this method:
function fileHandler(){ fs.writeFile('testFile.txt', "I'm the replacement you've been looking for.", (err) => { if(err) throw err; console.log('Data has been replaced!'); }); }
I'm the replacement you've been looking for.
fs
module provides the .readFile()
method, an example of the use of which we have already seen. As the first parameter, it takes the name of the file, as the second - the encoding. The third parameter is the callback function. We will try to output the contents of the testFile.txt
file to the console using this method:
function fileHandler(){ fs.readFile('testFile.txt', 'utf8', (err, data) => { if(err) throw err; console.log('--------- [File Data] ---------'); console.log(data); console.log('--------- [File Data] ---------'); }); }
.rename()
method:
function fileHandler(){ fs.rename('testFile.txt', 'newTestFile.txt', (err) => { if(err) throw err; console.log('File renamed successfully!'); }); }
testFile.txt
file testFile.txt
converted to newTestFile.txt
.
.unlink()
method:
function fileHandler(){ fs.unlink('newTestFile.txt', (err) => { if(err) throw err; console.log('File deleted successfully!'); }); }
newTestFile.txt
.
fs
module and try to practice everything you learn.
Source: https://habr.com/ru/post/452566/