欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Node.js中readline怎么實現(xiàn)逐行讀取、寫入文件內(nèi)容

本篇內(nèi)容主要講解“Node.js中readline怎么實現(xiàn)逐行讀取、寫入文件內(nèi)容”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習“Node.js中readline怎么實現(xiàn)逐行讀取、寫入文件內(nèi)容”吧!

目前成都創(chuàng)新互聯(lián)公司已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管維護、企業(yè)網(wǎng)站設(shè)計、和田網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

什么是Readline

Readline是Node.js里實現(xiàn)標準輸入輸出的封裝好的模塊,通過這個模塊我們可以以逐行的方式讀取數(shù)據(jù)流。使用require(“readline”)可以引用模塊。

效果圖如下:

左邊1.log 為源文件

右邊1.readline.log為復(fù)制后的文件

下邊為命令行輸出

Node.js中readline怎么實現(xiàn)逐行讀取、寫入文件內(nèi)容

實現(xiàn)方式一:

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, 
// 這是另一種復(fù)制方式,這樣on('line')里就不必再調(diào)用fWrite.write(line),當只是純粹復(fù)制文件時推薦使用 
// 但文件末尾會多算一次index計數(shù) 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...'); 
});

實現(xiàn)方式二:

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是先寫入后調(diào)用的on('line')事件, 
 // 所以已經(jīng)讀取文件完畢時就不需要再寫行號了... sodino.com 
 index ++; 
 var tmp = 'line' + index.toString() + ':'; 
 fWrite.write(tmp); 
 } 
}); 


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

到此,相信大家對“Node.js中readline怎么實現(xiàn)逐行讀取、寫入文件內(nèi)容”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習!

當前名稱:Node.js中readline怎么實現(xiàn)逐行讀取、寫入文件內(nèi)容
轉(zhuǎn)載來于:http://chinadenli.net/article6/jiojog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、網(wǎng)站設(shè)計、App設(shè)計微信小程序、品牌網(wǎng)站建設(shè)商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)