Detailed explanation of the difference of reading and writing file methods in nodeJS

  • 2021-07-26 06:35:43
  • OfStack

Introduction: All the operations related to files in nodejs are in fs module, and read and write operations are frequently used. fs module of nodejs provides us with three methods of readFile, read and createReadStream for read operations, and three methods of writeFile, write and createWriteStream for write operations. The following analysis shows their differences:

1. readFile and writeFile

1. readFile method is to read the file contents to be read into the cache area completely, and then read the file contents from the cache area. The specific operations are as follows:


fs.readFile('./test.txt', 'utf8', function(err, data){
  console.log(data); 
});

The corresponding synchronization method is:


var data = fs.readFileSync('./test.txt', 'utf8');
console.log(data);

The difference between a synchronous method and an asynchronous method is that the execution of subsequent code cannot be performed until the operation performed using the synchronous method is finished; The asynchronous method returns the operation result as the parameter of the callback function. After the method is called, the subsequent code can be executed immediately, and the corresponding callback function will be called after reading.

2. writeFile method is to read the contents of the file to be written into the cache completely, and then write the contents in the cache into all files once. Its synchronous and asynchronous operations are as follows:


// Asynchronous method 
fs.writeFile('./message.txt', ' This is the first 1 Row ',function(err){
  if(err) console.log(' Failed to write file operation ');
  else console.log(' Write file operation successful ');
});

// Synchronization method 
fs.writeFileSync('./message.txt',' This is the first 1 Row ');

For the above read and write operations, Node. js treats the file contents as a whole, allocates a buffer area for them, and reads the file contents into the buffer area once. During this period, Node. js will not be able to perform any other processing. Therefore, when reading and writing large files, it may cause "warehouse explosion" in the cache area.

2. read and write

1. The read or readSync method reads the file content by continuously reading a small piece of content in the file into the cache area, and finally reading the file content from the cache area. The specific operations are as follows:


var fs = require('fs');
fs.open('./message.txt','r',function(err,fd){
  var buf = new Buffer(225);
  // Read fd File contents to buf Buffer area 
  fs.read(fd,buf,0,9,3,function(err,bytesRead,buffer){
    console.log(buf.slice(0,bytesRead).toString());
  }); 
  var buff = new Buffer(225);
  // Location set to null Will be read from the current location of the file by default 
  fs.read(fd,buff,0,3,null,function(err,bytesRead,buffer){
    console.log(buff.slice(0,bytesRead).toString());
  });

  var buffer = new Buffer(225);
  // Synchronize method to read files 
  var bytesRead = fs.readFileSync(fd,buffer,0,9,3);
  console.log(bytesRead);
  console.log(buffer.slice(0,bytesRead).toString());
});

2. When write or writeSync method writes content, node. js executes the following procedures: 1. Writes the data to be written into a memory buffer area; 2 After the cache is full, write the contents in the cache to the file; 3 Repeat steps 1 and 2 until all the data is written to the file. The specific operation is as follows:


var fs = require('fs');
var buf = new Buffer(' I love programming ');
fs.open('./mess.txt','w',function(err,fd){
  fs.write(fd,buf,3,9,0,function(err,written,buffer){
    fs.write(fd,buf,12,3,null,function(err,written,buffer){
      if(err) console.log(' Failed to write file operation ');
      console.log(' Write file operation successful ');
    });
  });
  // Synchronous write 
  fs.writeSync(fd,buf,3,9,0);
});

For the above read and write operations, node. js will divide the file into one block and one block step by step, allowing other operations to be performed during the process of reading and writing files.

But sometimes we don't care about the contents of the whole file, but only about some data read from the file and the processing that needs to be performed when the data is read. In this case, we can use file stream to process it.

3. createReadStream and createWriteStream

1. The createReadStream method creates an ReadStream object that reads the contents of the file as stream data as follows:


var fs = require('fs');
var readStream = fs.createReadStream('./message.txt',{start:3,end:12});
readStream.on('open',function(fd){
  console.log(' Start reading files ');
});
readStream.on('data',function(data){
  console.log(' Read data: ');
  console.log(data);
});
readStream.on('end',function(){
  console.log(' All files have been read ');
});
readStream.on('close',function(){
  console.log(' File is closed ');
});
readStream.on('error',function(err){
  console.log(' Failed to read file ');
});

2. The createWriteStream method creates an WriteStream object that writes stream data to a file as follows:


var fs = require('fs');
var file = fs.createReadStream('./message.txt');
var out = fs.createWriteStream('./anotherMessage.txt');
file.on('data',function(data){
  out.write(data);
});
out.on('open',function(fd){
  console.log(' The file to be written is open ');
});
file.on('end',function(){
  // Write all the data in the operating system cache to the file 
  out.end(' See you later ',function(){
    console.log(' All files are written ');
    console.log(' Co-writing '+out.bytesWritten+' Data ');
  });
});

The above methods can monitor the process of reading and writing files, and define related methods pause and resume to suspend or resume the reading operation of files, and can monitor whether the buffer data is full or all output when writing, as follows:


// Eavesdropping writeStream Object's drain Events 
var fs = require('fs');
var out = fs.createWriteStream('./test1.txt');
for(var i=0;i<10000;i++){
  // Return true Or false true Represents that the cache is full 
  var flag = out.write(i.toString());
  console.log(flag);
}
out.on('drain',function(){
  console.log(' All the data in the operating system cache has been output ');
  var out = fs.createWriteStream('./test2.txt');
  for(var i=0;i<10;i++){
    var flag = out.write(i.toString());
    console.log(flag);
  }
  out.on('drain',function(){
    console.log(' All the data in the operating system cache has been output ');
  });
});

Related articles: