1. 使用readline模塊逐行讀取流數(shù)據(jù)
1.1. 創(chuàng)建Interface對(duì)象
在readline模塊中,通過(guò)Interface對(duì)象的使用來(lái)實(shí)現(xiàn)逐行讀取流數(shù)據(jù)的處理。因此首先要?jiǎng)?chuàng)建Interface對(duì)象,在readline模塊中,可以通過(guò)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)過(guò)格式化處理后的字符串.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)用程序的過(guò)程中非常有用.
+自定義util.inspect顏色
可以通過(guò)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,holyconsole.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:%sconsole.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í)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點(diǎn)
疑難解答