Home > Web Front-end > JS Tutorial > body text

Three ways to read and write files in nodeJS

零到壹度
Release: 2018-03-28 17:27:24
Original
7454 people have browsed it

All file-related operations in nodejs are in the fs module, and read and write operations are operations we often use. The fs module of nodejs provides us with readFile, read, createReadStream for read operations. Three methods, writeFile, write, and createWriteStream are provided for us for write operations. Let’s analyze their differences:

1. readFile and writeFile

1. The readFile method is to read the complete content of the file to be read into the cache area, and then read the file content from the cache area. The specific operation is as follows:

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

The corresponding synchronous method is:

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

The difference between synchronous methods and asynchronous methods is: before the operation performed using the synchronous method ends, no Execute the execution of subsequent code; the asynchronous method returns the operation result as a 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 the reading is completed.

2. The writeFile method is to read the complete content of the file to be written into the cache area, and then write the contents in the cache area to the file at one time. It is synchronous and asynchronous. The specific operations are as follows:


//异步方法
fs.writeFile('./message.txt', '这是第一行',function(err){  
  if(err) console.log('写文件操作失败');    
  else console.log('写文件操作成功');
});

//同步方法
fs.writeFileSync('./message.txt','这是第一行');
Copy after login

For the above read and write operations, Node.js treats the file content as a whole, allocates a cache area for it and writes it once The content of the file is read into the cache area. During this period, Node.js will not be able to perform any other processing. Therefore, when reading and writing large files, it may cause the cache area to "explode".

2. read and write

1. The read or readSync method reads the file content continuously. A small piece of content is read into the cache area, and finally the file content is read 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);    //读取fd文件内容到buf缓存区
    fs.read(fd,buf,0,9,3,function(err,bytesRead,buffer){
        console.log(buf.slice(0,bytesRead).toString());
    }); 
    var buff = new Buffer(225);    //位置设置为null会默认从文件当前位置读取
    fs.read(fd,buff,0,3,null,function(err,bytesRead,buffer){
        console.log(buff.slice(0,bytesRead).toString());
    });    var buffer = new Buffer(225);    //同步方法读取文件
    var bytesRead = fs.readFileSync(fd,buffer,0,9,3);
    console.log(bytesRead);
    console.log(buffer.slice(0,bytesRead).toString());
});
Copy after login

2. When the write or writeSync method writes content, node.js performs the following process: 1. Write the data that needs to be written into a memory cache area; 2. After the cache area is full, write the contents in the cache area to the file; 3. Repeat steps 1 and 2. , until all data is written to the file. The specific operations are as follows:

var fs = require('fs');var buf = new Buffer('我喜爱编程');
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('写文件操作失败');
            console.log('写文件操作成功');
        });
    });    //同步写入
    fs.writeSync(fd,buf,3,9,0);
});
Copy after login

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

但有的时候我们并不关心整个文件的内容,而只关注从文件中读取到的某些数据,以及读取到数据时需要执行的处理,这时我们可以使用文件流来处理。

三、createReadStream和createWriteStream

1、createReadStream方法创建一个将文件内容读取为流数据的ReadStream对象,方法如下所示:

var fs = require('fs');var readStream = fs.createReadStream('./message.txt',{start:3,end:12});
readStream.on('open',function(fd){
    console.log('开始读取文件');
});
readStream.on('data',function(data){
    console.log('读取到数据:');
    console.log(data);
});
readStream.on('end',function(){
    console.log('文件已全部读取完毕');
});
readStream.on('close',function(){
    console.log('文件被关闭');
});
readStream.on('error',function(err){
    console.log('读取文件失败');
});
Copy after login

2、createWriteStream方法创建一个将流数据写入文件中的WriteStream对象,方法如下所示:

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('需要被写入的文件已打开');
});
file.on('end',function(){    //将操作系统缓存区中的数据全部写入文件
    out.end('再见',function(){
        console.log('文件全部写入完毕');
        console.log('共写入'+out.bytesWritten+'数据');
    });
});
Copy after login

以上方法可以对读写文件的过程中进行监听,并定义相关的方法pause和resume暂停或恢复文件的读取操作,可以监听写入时缓存区数据是否已满或者是否已全部输出,如下所示:

//监听writeStream对象的drain事件var fs = require('fs');var out = fs.createWriteStream('./test1.txt');for(var i=0;i<10000;i++){    //返回true或false true代表缓存区已满
    var flag = out.write(i.toString());
    console.log(flag);
}
out.on('drain',function(){
    console.log('操作系统缓存区中的数据已全部输出');    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('操作系统缓存区中的数据已全部输出');
    });
});
Copy after login

The above is the detailed content of Three ways to read and write files in nodeJS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!