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. 用户人品预测大赛--TNT_000队--竞赛分享

     用户人品预测大赛--TNT_000队--竞赛分享  DataCastle运营 发表于 2016-3-24 14:29:57      887  0  0 答辩PPT 0 回复     用户反馈 隐私 ...

  2. Std::map too few template arguments

    在上述的代码中,红色波浪线的部分编译的时候报错: error C2976: 'std::map' : too few template arguments 换成std::map<std::str ...

  3. Emacs flycheck插件配置中遇到的若干问题

    工欲善其事必先利其器,一个高效的代码检查工具会大大提高我们的开发效率.flycheck是Emacs中常用的一个代码编译检查工具,本文记录配置它的时候遇到的一些问题以及解决方法. flycheck的基本 ...

  4. 查看修改添加环境变量的工具——Rapid Environment Editor

    工欲善其事,必先利其器! 特别是公司或者有其他限制的时候,更需要一个比较简单.实用.强大的工具了! 原来的公司都是小公司,给电脑安装系统.软件等都是自己直接上手,现在在一个大点的公司了,电脑运维有单独 ...

  5. [svc]linux下网桥-docker网桥

    网桥和交换机 2口交换机=网桥 交换机: 工作在数据链路层,根据源mac学习(控制层),目的mac转发(数据层). linux的网卡 vmware workstation中的桥接 参考: http:/ ...

  6. [Big Data - Kafka] Kafka设计解析(三):Kafka High Availability (下)

    Kafka是由LinkedIn开发的一个分布式的消息系统,使用Scala编写,它以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cloudera.Apache Storm.Spa ...

  7. 深入研究 Runloop 与线程保活

    深入研究 Runloop 与线程保活 在讨论 runloop 相关的文章,以及分析 AFNetworking(2.x) 源码的文章中,我们经常会看到关于利用 runloop 进行线程保活的分析,但如果 ...

  8. linux每日命令(26):Linux文件属性详解

    Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组.最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loca ...

  9. [转]深刻理解Python中的元类(metaclass)以及元类实现单例模式

    使用元类 深刻理解Python中的元类(metaclass)以及元类实现单例模式 在看一些框架源代码的过程中碰到很多元类的实例,看起来很吃力很晦涩:在看python cookbook中关于元类创建单例 ...

  10. GNU make使用(二)

    [时间:2017-06] [状态:Open] [关键词:makefile,gcc,编译,shell命令,目标文件] 0 引言及目标 之前使用Makefile都是把源文件和目标文件放到同一个目录编译.近 ...