node_readline (逐行读取)
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 (逐行读取)的更多相关文章
- android逐行读取文件内容以及保存为文件
用于长时间使用的apk,并且有规律性的数据 1,逐行读取文件内容 //首先定义一个数据类型,用于保存读取文件的内容 class WeightRecord { String timestamp; flo ...
- python中逐行读取文件的最佳方式_Drupal_新浪博客
python中逐行读取文件的最佳方式_Drupal_新浪博客 python中逐行读取文件的最佳方式 (2010-08-18 15:59:28) 转载▼ 标签: python ...
- php逐行读取txt文件写入数组的方法
使用说明: 采用fopen 方法,逐行读取数据,并使用feof($fp) 判断是否文件截止,最后通过filter() 方法,去除空白行,得到所需数据 $file = fopen("user ...
- Node.js 逐行读取
逐行读取 稳定性: 2 - 不稳定 使用 require('readline'),可以使用这个模块.逐行读取(Readline)可以逐行读取流(比如process.stdin) 一旦你开启了这个模块, ...
- 别再用"while (!feof(file))"来逐行读取txt文件了!
起因 执行一个C/C++程序出现segment fault.它逐行读取文本文件,每一行是一个图片名字,然后读图.处理图像,etc. 发现最后一次读取的文件名不存在(空的). 正确的逐行读取txt文件 ...
- python文件逐行读取四种方法
下面是四种Python逐行读取文件内容的方法, 并分析了各种方法的优缺点及应用场景,以下代码在python3中测试通过, python2中运行部分代码已注释,稍加修改即可. 方法一:readline函 ...
- 用python从符合一定格式的txt文档中逐行读取数据并按一定规则写入excel(openpyxl支持Excel 2007 .xlsx格式)
前几天接到一个任务,从gerrit上通过ssh命令获取一些commit相关的数据到文本文档中,随后将这些数据存入Excel中.数据格式如下图所示 观察上图可知,存在文本文档中的数据符合一定的格式,通过 ...
- PHP逐行读取数据
PHP逐行读取数据 <?php $file = fopen("Minot.txt", "r") or exit("Unable to open ...
- 【Node.js】'readline' 逐行读取、写入文件内容
[转]运用readline逐行读取的两种实现 效果图如下: 左边1.log 为源文件 右边1.readline.log为复制后的文件 下边为命令行输出 实现方式一: [javascript] view ...
随机推荐
- Android四大组件应用系列——使用ContentProvider实现跨进程通讯
一.问题描述 如何在Android中实现不同应用之间的通讯(既跨进程进行调用)?Android提供了多种实现方式,使我们可以实现跨进程访问Activity.通过ContentProvider跨进程访问 ...
- C# 6 的新特性~
原文地址 Mads Torgersen,微软 C# 程序管理者,它发布了一个视频,描述即将到来的下一个 C# 版本--C# 6.在 C# 6 的新特性之间,Mads 主要提到了 getter-only ...
- 视觉SLAM中的数学基础 第三篇 李群与李代数
视觉SLAM中的数学基础 第三篇 李群与李代数 前言 在SLAM中,除了表达3D旋转与位移之外,我们还要对它们进行估计,因为SLAM整个过程就是在不断地估计机器人的位姿与地图.为了做这件事,需要对变换 ...
- 使用Genymotion模拟器调试出现INSTALL_FAILED_CPU_ABI_INCOMPATIBLE错误的解决办法
如果遇到下面这种错误: 点击下载Genymotion-ARM-Translation.zip 百度云连接:http://pan.baidu.com/s/1o6ifjMM 将你的虚拟器启动起来,将下载好 ...
- 各类nosql db的功能与性能对比
mongodb cassandra neo4j couchbase couchdb redis leveldb
- Python CBV和FBV
''' 说一下CBV(class+base+views)和FBV(function+base+views),我们之前Django 里写的都是FBV,今天看一下 CBV,也就是在我们的视图函数里写类 ' ...
- ES Grafana
https://github.com/trevorndodds/elasticsearch-metrics https://grafana.com/dashboards/878 https://gra ...
- Cublas矩阵加速运算
前言 编写 CUDA 程序真心不是个简单的事儿,调试也不方便,很费时.那么有没有一些现成的 CUDA 库来调用呢? 答案是有的,如 CUBLAS 就是 CUDA 专门用来解决线性代数运算的库. 本文将 ...
- 推荐几个Windows工具软件: HDDB - Everything的替代品
主页: http://hddb.xp-zed.com/index.html 简单地说,HDDB 是NTFS磁盘的文件搜索工具,它最开始是模仿 Everything 的,但解决了 Everything ...
- 【iCore4 双核心板_ARM】例程七:WWDG看门狗实验——复位ARM
实验原理: STM32内部包含窗口看门狗,通过看门狗可以监控程序运行,程序运行错误时,未在 规定时间喂狗(提前或超时),自动复位ARM,本实验通过按键按下,停止喂狗,从而产 生复位. 核心代码: in ...