Home > Web Front-end > JS Tutorial > How readline reads and writes content line by line

How readline reads and writes content line by line

php中世界最好的语言
Release: 2018-04-12 15:10:22
Original
4239 people have browsed it

This time I will bring you readlineHow to read and write content line by line, readline read and write content line by lineNotes What are they? The following is a practical case. Let’s take a look.

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 renderings are as follows:

1.log on the left is the source file

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

The following is the command line 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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Vuex mutations and actions Detailed explanation of usage

How to use placeholders in Vue

The above is the detailed content of How readline reads and writes content 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