nodejs的readline怎么支持字符串逐行读取

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nodejs的readline怎么支持字符串逐行读取相关的知识,希望对你有一定的参考价值。

参考技术A var fs = require('fs'),
readline = require('readline');

var Stream = require('stream')
var stream = new Stream()

stream.pipe = function(dest)
dest.write(mailcontent)


stream.pipe(process.stdout)

console.log(process.input);

var rd = readline.createInterface(
input: stream,
output: process.stdout,
terminal: false
);

rd.on('line', function(line)
console.log(line);
);
会报错,如下:

readline.js:142
input.resume();
^
TypeError: Object #<Stream> has no method 'resume'
at new Interface (readline.js:142:9)
at Object.exports.createInterface (readline.js:39:10)
at Readable.<anonymous> (D:\mynodejs\node-server\mailUtil.js:66:47)
at Readable.g (events.js:180:16)
at Readable.EventEmitter.emit (events.js:92:17)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
参考技术B 用fs.createReadStream构造的没有问题。
var readline = require('readline'),
fs = require('fs');

var rl = readline.createInterface(
input: fs.createReadStream(__filename),
output: process.stdout,
terminal: false
);

rl.on('line', function(line)
console.log('> ' + line);
);

nodejs 文本逐行读写功能的实现

利用nodejs实现:逐行读写(从一个文件逐行复制到另外一个文件);逐行读取、处理和写入(读取一行,处理后,写入另一个文件)

1.所需要的模块: fs,os,readline

2.具体实现:

 a. 功能的实现:readWriteFileByLine.js

var fs = require(‘fs-extra‘);
var os = require(‘os‘);
var rl =  require(‘readline‘);

/**
 * 按行读写,无读取内容的处理,类似单纯的复制功能
 * @param {string} readName 
 * @param {string} writeName 
 */
var readWriteFileByLine = function(readName, writeName){
    var readStream =  fs.createReadStream(readName);
    var writeStream = fs.createWriteStream(writeName);
    var readLine = rl.createInterface({
        input: readStream,
        output: writeStream,
        terminal: true
    })
}

/**
 * 按行读写,中间包涵对读取的行内容的处理
 * @param {string} readName 
 * @param {string} writeName 
 * @param {Function} callback 
 */
var readWriteFileByLineWithProcess = function(readName,writeName,callback){
    var readStream = fs.createReadStream(readName);
    var writeStream = fs.createWriteStream(writeName);
    var readLine = rl.createInterface({
        input: readStream
    })
    readLine.on(‘line‘,function(line){
        var rs = callback(line);
        writeStream.write(rs + os.EOL);
    })
}

exports.readWriteFileByLine = readWriteFileByLine;
exports.readWriteFileByLineWithProcess = readWriteFileByLineWithProcess

  b. 功能的调用:index.js

var rwByLine = require(‘./lib/readWriteFileByLine.js‘)

//按行读写,对行做字符替换处理
var readName = ‘./obj.txt‘;
var writeName = ‘./rt.txt‘;
rwByLine.readWriteFileByLineWithProcess(readName,writeName,function(line){
    var rs = line.toString().replace("\",‘/‘);
    return rs;
})

//按行读写,无处理
readName = ‘./obj.txt‘;
writeName = ‘./rt1.txt‘;
rwByLine.readWriteFileByLine(readName,writeName);

 

以上是关于nodejs的readline怎么支持字符串逐行读取的主要内容,如果未能解决你的问题,请参考以下文章

java中readLine()方法为什么有的行读不到?

nodejs 文本逐行读写功能的实现

随机无限线程和各种错误,同时并行逐行读/写

node.jsreadline (逐行读取)

python处理txt文件的若干脚本

在nodejs中逐行读取文件没有简单的方法吗? [复制]