我想逐行读取文件,根据每行的数据,我必须对数据进行分类。
var rd = readline.createInterface({
input: fs.createReadStream('/home/user/Desktop/text.txt'),
output: process.stdout,
console: false
});
执行此行时,将在终端中读取并打印文件。
但是当我尝试使用readline逐行读取文件时,我收到错误。
rd.on('line', (input) => {
console.log(input);
});
我收到以下错误。
`Interface {
_sawReturnAt: 0,
isCompletionEnabled: true,
_sawKeyPress: false,
_previousKey:
{ sequence: '\n',
name: 'enter',
ctrl: false,
meta: false,
shift: false },
domain:
Domain {
domain: null,
_events: { error: [Function: debugDomainError] },
_eventsCount: 1,
_maxListeners: undefined,
members: [] },
_events: { line: [ [Function], [Function], [Function] ] },
_eventsCount: 1,
_maxListeners: undefined,
output:
WriteStream {
connecting: false,
_hadError: false,
_handle:
TTY {
bytesRead: 0,
_externalStream: {},
fd: 9,
writeQueueSize: 0,
owner: [Circular],
onread: [Function: onread] },
_parent: null,
_host: null,
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
domain: null,
_events:
{ end: [Object],
finish: [Function: onSocketFinish],
_socketEnd: [Function: onSocketEnd],
resize: [Object] },
_eventsCount: 4,
_maxListeners: undefined,
_writableState:
WritableState {
objectMode: false,
highWaterMark: 16384,
needDrain: false,
ending: false,
ended: false,
finished: false,
decodeStrings: false,
defaultEncoding: 'utf8',
length: 0,
writing: false,
corked: 0,
sync: false,
bufferProcessing: false,
onwrite: [Function: bound onwrite],
writecb: null,
writelen: 0,
bufferedRequest: null,
lastBufferedRequest: null,
pendingcb: 1,
prefinished: false,
errorEmitted: false,
bufferedRequestCount: 0,
corkedRequestsFree: [Object] },
writable: true,
allowHalfOpen: false,
destroyed: false,
_bytesDispatched: 22151,
_sockname: null,
_writev: null,
_pendingData: null,
_pendingEncoding: '',
server: null,
_server: null,
columns: 80,
rows: 24,
_type: 'tty',
fd: 1,
_isStdio: true,
destroySoon: [Function],
destroy: [Function] },
input:
ReadStream {
_readableState:
ReadableState {
objectMode: false,
highWaterMark: 65536,
buffer: [Object],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: true,
endEmitted: true,
reading: false,
sync: false,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: false,
domain:
Domain {
domain: null,
_events: [Object],
_eventsCount: 1,
_maxListeners: undefined,
members: [] },
_events: { end: [Object], data: [Function: onData] },
_eventsCount: 2,
_maxListeners: undefined,
path: '/home/user/Desktop/text.txt',
fd: null,
flags: 'r',
mode: 438,
start: undefined,
end: undefined,
autoClose: true,
pos: undefined,
bytesRead: 31,
destroyed: true,
closed: true },
historySize: 30,
crlfDelay: 100,
_prompt: '> ',
terminal: true,
line: '',
cursor: 0,
history: [ '1,2,3', '4,8,4,8', '2,2,2,2', '1,2,3,4' ],
historyIndex: -1,
prevRows: 0,
paused: true,
closed: true }`
请帮助我对此不熟悉。
答案 0 :(得分:1)
您的代码看起来不错,但您使用的是Node.js REPL,而不是运行文件中的代码,这就是您看到此行为的原因。
Node.js REPL(在终端中键入node
时获得的内容)将存储变量。但是,当仅使用标识符时,也返回该值。您所看到的不是错误,而是rd
对象。
Node.js REPL适合测试,但除非您有特定的理由使用它,否则最好的办法是创建一个app.js文件,将代码添加到其中,然后运行node app.js
如果确实需要使用REPL编写多行代码,请确保在初始化后键入.editor
,以便正确解释空格等。
此外,来自7.7.2文档console
并不是createInterface
期待的选项,因此可以将其删除。在您的特定示例中,您还可以删除output: process.stdout
,因为您使用line
事件记录每一行