Home > Web Front-end > JS Tutorial > How to use readline in Node.js to read and write file contents line by line

How to use readline in Node.js to read and write file contents line by line

亚连
Release: 2018-06-02 11:44:43
Original
2908 people have browsed it

This article mainly introduces examples of Node.js readline reading and writing file content line by line. Two implementations of using readline to read line by line are shared with you now and can be used as a reference.

This article introduces two implementations of line-by-line reading using readline and shares them with everyone. The details are as follows:

What is Readline

Readline is A packaged module that implements standard input and output in Node.js. Through this module, we can read the data stream line by line. Modules can be referenced using require("readline").

The rendering is as follows:

1.log on the left is the source file

1.readline.log on the right is the copied file

The command line is on the bottom Output

Implementation method one:

var readline = require('readline'); 
var fs = require('fs'); 
var os = require('os'); 
var fReadName = './1.log'; 
var fWriteName = './1.readline.log'; 
var fRead = fs.createReadStream(fReadName); 
var fWrite = fs.createWriteStream(fWriteName); 
var objReadline = readline.createInterface({ 
 input: fRead, 
// 这是另一种复制方式,这样on('line')里就不必再调用fWrite.write(line),当只是纯粹复制文件时推荐使用 
// 但文件末尾会多算一次index计数 sodino.com 
// output: fWrite, 
// terminal: true 
}); 
 
 
var index = 1; 
objReadline.on('line', (line)=>{ 
 var tmp = 'line' + index.toString() + ':' + line; 
 fWrite.write(tmp + os.EOL); // 下一行 
 console.log(index, line); 
 index ++; 
}); 
 
objReadline.on('close', ()=>{ 
 console.log('readline close...'); 
});
Copy after login

Implementation method two:

var readline = require('readline'); 
var fs = require('fs'); 
var os = require('os'); 
 
var fReadName = './1.log'; 
var fWriteName = './1.readline.log'; 
var fRead = fs.createReadStream(fReadName); 
var fWrite = fs.createWriteStream(fWriteName); 
 
var enableWriteIndex = true; 
fRead.on('end', ()=>{ 
 console.log('end'); 
 enableWriteIndex = false; 
}); 
 
var objReadline = readline.createInterface({ 
 input: fRead, 
 output: fWrite, 
 terminal: true 
}); 
 
var index = 1; 
fWrite.write('line' + index.toString() +':'); 
objReadline.on('line', (line)=>{ 
 console.log(index, line); 
 if (enableWriteIndex) { 
 // 由于readline::output是先写入后调用的on('line')事件, 
 // 所以已经读取文件完毕时就不需要再写行号了... sodino.com 
 index ++; 
 var tmp = 'line' + index.toString() + ':'; 
 fWrite.write(tmp); 
 } 
}); 


objReadline.on('close', ()=>{ 
 console.log('readline close...'); 
});
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Using webpack module to package Library Principle and implementation

In-depth explanation of the basic principles of webpack module

How to use Vuex to implement the counter function

The above is the detailed content of How to use readline in Node.js to read and write file contents line by line. 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