Node.js readline丢失最后一行文件? [英] Node.js readline missing last line of file?
本文介绍了Node.js readline丢失最后一行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在Node.js下异步和逐行读取文本文件。我有以下CoffeeScript代码:
I'm trying to read a text file asynchronously and line-by-line under Node.js. I have the following CoffeeScript code:
readline = require 'readline'
fs = require 'fs'
#--------------------------------------------------------------------
lines_of = ( route, handler ) ->
#..................................................................
stream = readline.createInterface
input: fs.createReadStream route
output: process.stdout
terminal: false
#..................................................................
stream.on 'close', ->
handler null, null
#..................................................................
stream.on 'error', ( error ) ->
handler error
#..................................................................
stream.on 'line', ( line ) ->
handler null, line
除非最后一行文件不报告除非它由换行符终止。是否有任何方法来补救(除非手动添加换行符)?
It works fine except the very last line of files is never reported unless it is terminated by a newline. is there any way to remedy this (except, of course, adding the newline manually)?
推荐答案
c $ c> fs 模块,并分割 \\\
chars上的文件内容以确定行结束的位置。
You should use the fs
module for this, and split the file contents on \n
chars to determine where lines end.
这篇关于Node.js readline丢失最后一行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文