本篇文章主要介绍了Node.js readline模块与util模块的使用,写的十分的全面细致,具备必定的参考价值,对此有须要的朋友能够参考学习下。若有不足之处,欢迎批评指正。css
#1. 使用readline模块逐行读取流数据html
1.1. 建立Interface对象前端
在readline模块中,经过Interface对象的使用来实现逐行读取流数据的处理。所以首先要建立Interface对象,在readline模块中,能够经过createInterface方法来建立Interface对象.readline.createInterface(options),options为一个对象,属性以下vue
// 输入 exit, quit,q这三个任意之一的时候,会退出
const readline = require('readline');
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
completer: completer
});//欢迎加入前端全栈开发交流圈一块儿吹水聊天学习交流:864305860
rl.on('line', (line) => {
if (line === 'exit' || line === 'quit' || line === 'q') {
rl.close();
} else {
console.log('您输入了:', line);
}
});
rl.on('close', () => {
console.log('行数据读取操做被终止');
});
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]
}//欢迎加入前端全栈开发交流圈一块儿吹水聊天学习交流:864305860
复制代码
1.2. 使用Interface对象逐行读取文件 原fs.js文件的内容node
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');
复制代码
代码内容webpack
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() + ': */');
}//欢迎加入前端全栈开发交流圈一块儿吹水聊天学习交流:864305860
});
复制代码
生成的anotherFs.js文件的内容web
/*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语言中的printf方法,将第一个参数值做为一个格式化字符串,将其余参数值做为该格式化字符串中所使用的各中参数,返回一个通过格式化处理后的字符串.util.format('您输入了%d个参数,参数值分别为%s,%s,%s',3,'nice','excelent','holy'); 格式化字符串中,可使用的参数指定符号面试
%s
:用于指定字符串参数%d
:用于指定数值参数,包括整数及浮点数%j
:用于指定一个JSON
对象%%
:用于指定一个百分号format
参数以外的其余参数,则格式化字符串中多于的参数将不被替换.console.log(util.format('%s:%s','one'));
format
方法中使用的除了format
参数以外的其余参数,则根据format
方法中多于参数值的类型自动将其转换为字符串,中间使用一个空格进行分割. +inspect(object,[options])返回一个字符串,该字符串包含了对象的信息,在调试应用程序的过程当中很是有用.showHidden<boolean>
若是为true
,则object
的不可枚举的符号与属性也会被包括在格式化后的结果中.默认为false.
depth<number>
指定格式化object
时递归的次数.这对查看大型复杂对象颇有用.默认为2
.若要无限地递归则传入null
.colors<boolean>
若是为true
,则输出样式使用ANSI
颜色代码.默认为false
.颜色可自定义.customInspect<boolean>
若是为false
,则object
上自定义的inspect(depth,opts)
函数不会被调用.默认为true
.showProxy<boolean>
若是为true
,则Proxy
对象的对象和函数会展现它们的target
和handler
对象.默认为false
.maxArrayLength<number>
指定格式化时数组和TypedArray
元素能包含的最大数量.默认为100
.设为null
则显式所有数组元素.设为0*
或负数则不显式数组元素.breakLength<number>
一个对象的键被拆分红多行的长度.设为Infinity
则格式化一个对象为单行.默认为60
. +自定义util.inspect颜色 能够经过util.inspect.styles和util.inspect.colors属性全局地自定义util.inspect的颜色输出(若是已启用)const util = require('util');
console.log(util.format('您输入了%d个参数,参数值分别为%s,%s,%s', 3, 'nice', 'excelent', 'holy'));
//您输入了3个参数,参数值分别为nice,excelent,holy
console.log(util.format('一个JSON对象%j', {'name': 'jack', 'age': 25}));
// 一个JSON对象{"name":"jack","age":25}
console.log(util.format('一个百分号%'));// 一个百分号%
console.log(util.format('%s:%s', 'one'));// one:%s
console.log(util.format('%s', 'one', 'two', 'three', {'name': 'jack'}));
//欢迎加入前端全栈开发交流圈一块儿吹水聊天学习交流:864305860
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] } } }
* **///欢迎加入前端全栈开发交流圈一块儿吹水聊天学习交流:864305860
复制代码
结语数组
感谢您的观看,若有不足之处,欢迎批评指正。bash
本次给你们推荐一个免费的学习群,里面归纳移动应用网站开发,css,html,webpack,vue node angular以及面试资源等。 对web开发技术感兴趣的同窗,欢迎加入Q群:864305860,无论你是小白仍是大牛我都欢迎,还有大牛整理的一套高效率学习路线和教程与您免费分享,同时天天更新视频资料。 最后,祝你们早日学有所成,拿到满意offer,快速升职加薪,走上人生巅峰。