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. spring mvc 异常统一处理

    摘自: http://gaojiewyh.iteye.com/blog/1297746  

  2. .NET 用 Unity 依赖注入——概述注册和解析类型(1)

    本文内容 Unity 概述 环境 一个真实的例子 类型注册(Type Registrations) 解析类型(Resolving Types) 跳槽,新公司使用了 Unity,初步看了一下,公司的使用 ...

  3. from String value ('{}'); no single-String constructor/factory

    需要为类增加一个接受String的构造函数: 例如: public class B { private String name; public B(String b) { } public Strin ...

  4. 关于select标签曾经踩过的几个坑!

      1.情景展示 select标签,是前端开发界面展示,经常需要用到一个标签,看看下面的坑,你中招了吗? 2.坑区展示 踩坑一:option标签没有声明value属性: 没有声明value属性 < ...

  5. Junit学习笔记之五:MockMVC

    原文:https://blog.csdn.net/xiao_xuwen/article/details/52890730 随着RESTful Web Service的流行,测试对外的Service是否 ...

  6. HDU 4666 Hyperspace (最远曼哈顿距离)

    Hyperspace Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  7. Boinx FotoMagico for Mac(电子相册制作工具)破解版安装

    1.软件简介    FotoMagico 是 macOS 系统上一款非常好用的电子视频相册制作工具,FotoMagico 被誉为 Mac 上的「会声会影」,我们可以使用这款软件快速的制作出精美的音乐视 ...

  8. OpenCV 学习笔记 05 级联分类器CascadeClassifier类

    在人脸检测中,CascadeClassifier 是一个类,该类的作用是(基于官方已经训练好的数据文件 .xml)实例化一个检测器. 1 类 CascadeClassifier 的概述 首先看一下该类 ...

  9. 12C -- ORA-01017

    本地使用使用sqlplus,尝试连接12.2数据库报错: 在另外一台服务器上,使用sqlplus连接该库,可以成功: 解决方案: 根据MOS文档id:207303.1看出,只有11.2.0.3之上的客 ...

  10. BigDecimal乘法

    BigDecimal result = new BigDecimal(doubleValue).multiply(factor2); public class Payment { BigDecimal ...