1. 使用readline模塊逐行讀取流數(shù)據(jù)
創(chuàng)新互聯(lián)客戶idc服務(wù)中心,提供德陽(yáng)機(jī)房服務(wù)器托管、成都服務(wù)器、成都主機(jī)托管、成都雙線服務(wù)器等業(yè)務(wù)的一站式服務(wù)。通過各地的服務(wù)中心,我們向成都用戶提供優(yōu)質(zhì)廉價(jià)的產(chǎn)品以及開放、透明、穩(wěn)定、高性價(jià)比的服務(wù),資深網(wǎng)絡(luò)工程師在機(jī)房提供7*24小時(shí)標(biāo)準(zhǔn)級(jí)技術(shù)保障。
1.1. 創(chuàng)建Interface對(duì)象
在readline模塊中,通過Interface對(duì)象的使用來(lái)實(shí)現(xiàn)逐行讀取流數(shù)據(jù)的處理。因此首先要?jiǎng)?chuàng)建Interface對(duì)象,在readline模塊中,可以通過createInterface方法來(lái)創(chuàng)建Interface對(duì)象.readline.createInterface(options),options為一個(gè)對(duì)象,屬性如下
// 輸入 exit, quit,q這三個(gè)任意之一的時(shí)候,會(huì)退出 const readline = require('readline'); let rl = readline.createInterface({ input: process.stdin, output: process.stdout, completer: completer }); rl.on('line', (line) => { if (line === 'exit' || line === 'quit' || line === 'q') { rl.close(); } else { console.log('您輸入了:', line); } }); rl.on('close', () => { console.log('行數(shù)據(jù)讀取操作被終止'); }); function completer(line) { const completions = '.help .error .exit .quit .q'.split(' '); let hits = completions.filter((c) => { return c.indexOf(line) === 0; }); return [hits.length ? hits : completions, line] }
1.2. 使用Interface對(duì)象逐行讀取文件
原fs.js文件的內(nèi)容
console.log('this is line 1'); console.log('this is line 2'); console.log('this is line 3'); console.log('this is line 4'); console.log('this is line 5');
代碼內(nèi)容
const readline = require('readline'); const fs = require('fs'); let file = fs.createReadStream('./fs.js'); let out = fs.createWriteStream('./anotherFs.js'); let index = 1; out.write('/*line' + index.toString() + ": */"); let rl = readline.createInterface({ input: file, output: out, terminal: true }); rl.on('line', (line) => { if (line === '') { rl.close(); } else { index++; out.write('/*line' + index.toString() + ': */'); } });
生成的anotherFs.js文件的內(nèi)容
/*line1: */console.log('this is line 1'); /*line2: */console.log('this is line 2'); /*line3: */console.log('this is line 3'); /*line4: */console.log('this is line 4'); /*line5: */console.log('this is line 5');/*line6: */
2. 使用util模塊中提供的一些方法
+format方法
類似于C語(yǔ)言中的printf方法,將第一個(gè)參數(shù)值作為一個(gè)格式化字符串,將其他參數(shù)值作為該格式化字符串中所使用的各中參數(shù),返回一個(gè)經(jīng)過格式化處理后的字符串.util.format('您輸入了%d個(gè)參數(shù),參數(shù)值分別為%s,%s,%s',3,'nice','excelent','holy');
格式化字符串中,可以使用的參數(shù)指定符號(hào)
+inspect(object,[options])返回一個(gè)字符串,該字符串包含了對(duì)象的信息,在調(diào)試應(yīng)用程序的過程中非常有用.
+自定義util.inspect顏色
可以通過util.inspect.styles和util.inspect.colors屬性全局地自定義util.inspect的顏色輸出(如果已啟用)
const util = require('util'); console.log(util.format('您輸入了%d個(gè)參數(shù),參數(shù)值分別為%s,%s,%s', 3, 'nice', 'excelent', 'holy')); //您輸入了3個(gè)參數(shù),參數(shù)值分別為nice,excelent,holy console.log(util.format('一個(gè)JSON對(duì)象%j', {'name': 'jack', 'age': 25})); // 一個(gè)JSON對(duì)象{"name":"jack","age":25} console.log(util.format('一個(gè)百分號(hào)%'));// 一個(gè)百分號(hào)% console.log(util.format('%s:%s', 'one'));// one:%s console.log(util.format('%s', 'one', 'two', 'three', {'name': 'jack'})); function test(one, two) { return one + two; } let parent = new Object(); parent.name = 'parent'; parent.func = test; let child1 = new Object(); child1.name = 'child1'; parent.child1 = child1; let child2 = new Object(); child2.name = 'child2'; child1.child = child2; let child3 = new Object(); child3.name = 'child3'; child2.child = child3; child2.inspect = function (depth) { return util.inspect(this, {depth: depth - 2, customInspect: false}) }; console.log(util.inspect(parent, {customInspect: true, depth: 4})); /** * { name: 'parent', * func: [Function: test], * child1: * { name: 'child1', * child: { name: 'child2', child: [Object], inspect: [Function] } } } * **/
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前文章:Node.jsreadline模塊與util模塊的使用
URL鏈接:http://www.muchs.cn/article12/gehidc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站制作、微信小程序、、外貿(mào)網(wǎng)站建設(shè)、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)