【Node.js】'readline' 逐行读取、写入文件内容
[转]运用readline逐行读取的两种实现
效果图如下:
左边1.log 为源文件
右边1.readline.log为复制后的文件
下边为命令行输出
实现方式一:
- var readline = require('readline');
- var fs = require('fs');
- var os = require('os');
- var fReadName = './1.log';
- var fWriteName = './1.readline.log';
- var fRead = fs.createReadStream(fReadName);
- var fWrite = fs.createWriteStream(fWriteName);
- var objReadline = readline.createInterface({
- input: fRead,
- // 这是另一种复制方式,这样on('line')里就不必再调用fWrite.write(line),当只是纯粹复制文件时推荐使用
- // 但文件末尾会多算一次index计数 sodino.com
- // output: fWrite,
- // terminal: true
- });
- var index = 1;
- objReadline.on('line', (line)=>{
- var tmp = 'line' + index.toString() + ':' + line;
- fWrite.write(tmp + os.EOL); // 下一行
- console.log(index, line);
- index ++;
- });
- objReadline.on('close', ()=>{
- console.log('readline close...');
- });
实现方式二:
- var readline = require('readline');
- var fs = require('fs');
- var os = require('os');
- var fReadName = './1.log';
- var fWriteName = './1.readline.log';
- var fRead = fs.createReadStream(fReadName);
- var fWrite = fs.createWriteStream(fWriteName);
- var enableWriteIndex = true;
- fRead.on('end', ()=>{
- console.log('end');
- enableWriteIndex = false;
- });
- var objReadline = readline.createInterface({
- input: fRead,
- output: fWrite,
- terminal: true
- });
- var index = 1;
- fWrite.write('line' + index.toString() +':');
- objReadline.on('line', (line)=>{
- console.log(index, line);
- if (enableWriteIndex) {
- // 由于readline::output是先写入后调用的on('line')事件,
- // 所以已经读取文件完毕时就不需要再写行号了... sodino.com
- index ++;
- var tmp = 'line' + index.toString() + ':';
- fWrite.write(tmp);
- }
- });
- objReadline.on('close', ()=>{
- console.log('readline close...');
- });
【Node.js】'readline' 逐行读取、写入文件内容的更多相关文章
- php逐行读取.txt文件内容,并解析每行内容
// 读取nlp text 并存到mongodb public function readNLP(&$errorCode,&$errorMessage) { try{ // $_SER ...
- Node.js高效按行输出文件内容
const fs = require('fs'); const EventEmitter = require('events'); const util = require('util'); cons ...
- 手工创建tomcat应用,以及实现js读取本地文件内容
手工创建tomcat应用: 1.在webapps下面新建应用目录文件夹 2.在文件夹下创建或是从其他应用中复制:META-INF,WEB-INF这两个文件夹, 其中META-INF清空里面,WEB-I ...
- 关于Python中读取写入文件并进行文件与用户交互的操作
一.提前知识点 在python中是同样和其他语言一样可以进行文件的读取写入操作,值得注意的是,Python中打开文件读取的方式有几种,分别是以下几种: f = open('username.txt') ...
- Java基础面试操作题:读取该文件内容,并按照自然顺序排序后输出到 另一个文件中
package com.swift; import java.io.FileInputStream; import java.io.FileNotFoundException; import java ...
- 在 Node.js 中处理大 JSON 文件
在 Node.js 中处理大 JSON 文件 场景描述 问题一: 假设现在有一个场景,有一个大的 JSON 文件,需要读取每一条数据经过处理之后输出到一个文件或生成报表数据,怎么能够流式的每次读取一条 ...
- 读取Zip文件内容
第一步,上次文件并保存到服务器目录下 /// <summary> /// 上传压缩文件 /// </summary> protected void UploadZip() { ...
- java读取txt文件内容
package read; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public ...
- 别再用"while (!feof(file))"来逐行读取txt文件了!
起因 执行一个C/C++程序出现segment fault.它逐行读取文本文件,每一行是一个图片名字,然后读图.处理图像,etc. 发现最后一次读取的文件名不存在(空的). 正确的逐行读取txt文件 ...
随机推荐
- 【BZOJ1922】[Sdoi2010]大陆争霸 Dijkstra
Description 具体地说,杰森国有 N 个城市,由 M条单向道 路连接.神谕镇是城市 1而杰森国的首都是城市 N.你只需摧毁位于杰森国首都 的曾·布拉泽大神殿,杰森国的信仰,军队还有一切就都会 ...
- GetDesktopWindow和GetWindow区别
GetWindow The GetWindow function retrieves a handle to a window that has the specified relationship ...
- 160811、29 个你必须知道的 Linux 命令
虽然Linux发行版支持各种各样的饿GUI(graphical user interfaces),但在某些情况下,Linux的命令行接口(bash)仍然是简单快速的.Bash和 Linux Shell ...
- Unix file types
w https://en.wikipedia.org/wiki/Unix_file_types A socket is a special file used for inter-process co ...
- json/pickle/shelve/xml/configparser/hashlib/subprocess - 总结
序列化:序列化指把内存里的数据类型转成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes为什么要序列化:可以直接把内存数据(eg:10个列表,3个嵌套字典)存到硬盘 ...
- JSONObject和JSONArray 以及Mybatis传入Map类型参数
import org.json.JSONArray;import org.json.JSONObject; 将字符串转化为JSONArray JSONArray jsonArray = new ...
- Redis六(管道)
管道 为什么使用管道? Redis是一个TCP服务器,支持请求/响应协议. 在Redis中,请求通过以下步骤完成: 客户端向服务器发送查询,并从套接字读取,通常以阻塞的方式,用于服务器响应. 服务器处 ...
- jQuery Mobile 手动显示ajax加载器
在jquery mobile开发中,经常需要调用ajax方法,异步获取数据,如果异步获取数据方法由于网速等等的原因,会有一个反应时间,如果能在点击按钮后数据处理期间,给一个正在加载的提示,客户体验会更 ...
- FileZilla Server IP限制设置
上面那个是黑名单. * 代表所有 下面那个是白名单. 多个是用 空格 分割
- httpmessageconverter requestbody responsebody
@ResponseBody @RequestMapping("/testHttpMessageConverter") public String testHttpMessageCo ...