node官方文档

用于逐行读取文件流, 和终端交互

读取文件流

const fs = require('fs');
const readline = require('readline'); var rl = readline.createInterface({
input: fs.createReadStream('a.js'),
crlfDelay: Infinity
}); rl.on('line', lineFd =>{
console.log( lineFd);
}); // print
第一行
第2行
第3行
第4行

终端交互

const readline = require('readline');
const util = require('util'); const rl = readline.createInterface({input: process.stdin, output: process.stdout}); function random(min, max) {
return Math.random() * (max - min + 1)
} function logger(target, obj = {}) {
var inspect = util.inspect;
if (typeof obj === 'object') {
Object.assign(inspect.styles, obj);
} else {
Object.assign(inspect.styles, {
[typeof target]: obj
});
}
return inspect(target, {colors: true}).replace(/'/g, '');
} class flEvent {
constructor() {} // 流被暂停
pause() {
rl.on('pause', () => {});
} // 流被恢复
resume() {
rl.on('resume', () => {
this.downloadTimer = setInterval(() => this.downloadFeedback(), this.t);
});
}
} // 模拟下载文件
class Test extends flEvent {
constructor() {
super();
this.progress = 1;
this.downloadTimer;
this.t = 1000 * 1;
}
run() {
this.sigint();
this.pause();
this.resume(); this.hello();
} // 是否确定下载
hello() {
rl.question( logger('你确定下载这个文件吗? yes/no >', 'green'), data => {
if (this.isDownload(data)) {
rl.close();
} else {
this.downloading()
}
})
} // 下载中
downloading() {
rl.write(`开始下载! ${logger(this.progress, 'yellow')}% \n`);
// 每隔1秒 更与用户反馈
this.downloadTimer = setInterval(() => this.downloadFeedback(), this.t);
} // 监听ctrl+c的退出事件
sigint() {
rl.on('SIGINT', () => {
clearInterval(this.downloadTimer);
util.inspect.styles.string = 'red'
rl.question( logger("确定要退出下载吗?yes/no >", 'red'), data => {
rl.pause();
if (!this.isDownload(data)) {
rl.close(); // yes关闭流
return;
} else {
rl.resume(); // no重启流
}
});
});
}
downloadFeedback() {
var num = Math.floor(random(10, 20));
this.progress += num;
if (this.isDownloadOk(this.progress)) {
rl.write(`正在下载! ${logger('100', 'green')}% \n`);
rl.write( logger('全部下载完成!! ^_^', 'green') );
rl.close();
clearInterval(this.downloadTimer);
this.downloadTimer = num = null;
return;
}
rl.write(`正在下载! ${logger(this.progress, 'yellow')}% \n`);
} isDownload(data) {
if (data.match(/^y(es)?$/i)) {
return false;
} else {
return true;
}
} isDownloadOk(num) {
if (num >= 100) {
return true;
} else {
return false;
}
}
} var test = new Test();
test.run();

node_readline (逐行读取)的更多相关文章

  1. android逐行读取文件内容以及保存为文件

    用于长时间使用的apk,并且有规律性的数据 1,逐行读取文件内容 //首先定义一个数据类型,用于保存读取文件的内容 class WeightRecord { String timestamp; flo ...

  2. python中逐行读取文件的最佳方式_Drupal_新浪博客

    python中逐行读取文件的最佳方式_Drupal_新浪博客 python中逐行读取文件的最佳方式    (2010-08-18 15:59:28)    转载▼    标签:    python   ...

  3. php逐行读取txt文件写入数组的方法

    使用说明: 采用fopen 方法,逐行读取数据,并使用feof($fp)  判断是否文件截止,最后通过filter() 方法,去除空白行,得到所需数据 $file = fopen("user ...

  4. Node.js 逐行读取

    逐行读取 稳定性: 2 - 不稳定 使用 require('readline'),可以使用这个模块.逐行读取(Readline)可以逐行读取流(比如process.stdin) 一旦你开启了这个模块, ...

  5. 别再用"while (!feof(file))"来逐行读取txt文件了!

    起因 执行一个C/C++程序出现segment fault.它逐行读取文本文件,每一行是一个图片名字,然后读图.处理图像,etc. 发现最后一次读取的文件名不存在(空的). 正确的逐行读取txt文件 ...

  6. python文件逐行读取四种方法

    下面是四种Python逐行读取文件内容的方法, 并分析了各种方法的优缺点及应用场景,以下代码在python3中测试通过, python2中运行部分代码已注释,稍加修改即可. 方法一:readline函 ...

  7. 用python从符合一定格式的txt文档中逐行读取数据并按一定规则写入excel(openpyxl支持Excel 2007 .xlsx格式)

    前几天接到一个任务,从gerrit上通过ssh命令获取一些commit相关的数据到文本文档中,随后将这些数据存入Excel中.数据格式如下图所示 观察上图可知,存在文本文档中的数据符合一定的格式,通过 ...

  8. PHP逐行读取数据

    PHP逐行读取数据 <?php $file = fopen("Minot.txt", "r") or exit("Unable to open ...

  9. 【Node.js】'readline' 逐行读取、写入文件内容

    [转]运用readline逐行读取的两种实现 效果图如下: 左边1.log 为源文件 右边1.readline.log为复制后的文件 下边为命令行输出 实现方式一: [javascript] view ...

随机推荐

  1. 用户人品预测大赛--getmax队--竞赛分享

     用户人品预测大赛--getmax队--竞赛分享  DataCastle运营 发表于 2016-3-24 14:49:32      533  0  0 答辩PPT  

  2. grid - 通过网格线号码来定位网格项目

    网格线实际上是代表线的开始.结束. 两者之间就是网格列或行. 以下css仅对子元素生效 ,具体详情可以看后面示例 grid-row-start: 2; grid-row-end: 3; grid-co ...

  3. 一道笔试题:给定编码规则,实现decode()方法

    public class CodeDecode {     /*变换函数encode()顺序考察已知字符串的字符,按以下规则逐组生成新字符串:       (1)若已知字符串的当前字符不是大于0的数字 ...

  4. [转]抛弃jQuery,使用原生JavaScript

    原文链接 Document Ready 事件 在jQuery中,document.ready可以让代码在整个文档加载完毕之后执行: $(document).ready(function() { // ...

  5. 爬虫 需要什么样的 CPU,内存 和带宽

    所有的需求都看这个图片吧,这个就是我爬取一个网站所用的服务器和服务器资源的消耗情况.

  6. C# 版本的24点实现

    C# 版本的24点实现. 已经实现基本功能,可以正确的算 3, 3, 8, 8 这类组合. 稍加修改就可以支持任意数目的操作数和操作符组合形成的四则运算表达式,不限于24点. 代码还比较简单粗糙,晚一 ...

  7. Socket网络编程--小小网盘程序(2)

    这一节将不会介绍太多的技术的问题,这节主要是搭建一个小小的框架,为了方便接下来的继续编写扩展程序.本次会在上一小节的基础上加上一个身份验证的功能. 因为网盘程序不像聊天程序,网盘是属于主动向服务器拉取 ...

  8. sublime text 3 安装ES6插件

  9. linux每日命令(28):chgrp命令

    在linux系统里,文件或目录的权限的掌控以拥有者及所属群组来管理.可以使用chgrp指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别码都可以.Chgrp命令就是change group的 ...

  10. Vue中使用ECharts画散点图加均值线与阴影区域

    [本文出自天外归云的博客园] 需求 1. Vue中使用ECharts画散点图 2. 在图中加入加均值线 3. 在图中标注出阴影区域 实现 实现这个需求,要明确两点: 1. 知道如何在vue中使用ech ...