我想把一个非常非常大的文件读入node。js中的JavaScript数组。
所以,如果文件是这样的:
first line
two
three
...
...
我有一个数组:
['first line','two','three', ... , ... ]
函数看起来是这样的:
var array = load(filename);
因此,将其全部作为字符串加载,然后将其拆分的想法是不可接受的。
我想把一个非常非常大的文件读入node。js中的JavaScript数组。
所以,如果文件是这样的:
first line
two
three
...
...
我有一个数组:
['first line','two','three', ... , ... ]
函数看起来是这样的:
var array = load(filename);
因此,将其全部作为字符串加载,然后将其拆分的想法是不可接受的。
当前回答
为了将每行作为数组中的一项,在Node.js v18.11.0中添加了一个逐行读取文件的新函数
filehandle.readLines([选项])
这就是如何将此用于文本文件,您希望读取文件并将每行放入数组中
import { open } from 'node:fs/promises';
const arr = [];
myFilereader();
async function myFileReader() {
const file = await open('./TextFileName.txt');
for await (const line of file.readLines()) {
arr.push(line);
}
console.log(arr)
}
为了了解更多read Node.js文档,这里有文件系统readlines()的链接: https://nodejs.org/api/fs.html#filehandlereadlinesoptions
其他回答
使用Node.js的readline模块。
var fs = require('fs');
var readline = require('readline');
var filename = process.argv[2];
readline.createInterface({
input: fs.createReadStream(filename),
terminal: false
}).on('line', function(line) {
console.log('Line: ' + line);
});
使用BufferedReader,但函数应该是异步的:
var load = function (file, cb){
var lines = [];
new BufferedReader (file, { encoding: "utf8" })
.on ("error", function (error){
cb (error, null);
})
.on ("line", function (line){
lines.push (line);
})
.on ("end", function (){
cb (null, lines);
})
.read ();
};
load ("file", function (error, lines){
if (error) return console.log (error);
console.log (lines);
});
如果你能把最终的数据放入一个数组,那么你不也能把它放入一个字符串中并分割它吗? 在任何情况下,如果你想一次处理一行文件,你也可以尝试这样做:
var fs = require('fs');
function readLines(input, func) {
var remaining = '';
input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
while (index > -1) {
var line = remaining.substring(0, index);
remaining = remaining.substring(index + 1);
func(line);
index = remaining.indexOf('\n');
}
});
input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
});
}
function func(data) {
console.log('Line: ' + data);
}
var input = fs.createReadStream('lines.txt');
readLines(input, func);
编辑:(回应phopkins的评论)我认为(至少在新版本中)substring不复制数据,而是创建一个特殊的SlicedString对象(从v8源代码中快速浏览)。在任何情况下,这里有一个修改,避免了提到的子字符串(在一个文件上测试了几个兆字节的“只工作不玩耍,聪明的孩子会变傻”):
function readLines(input, func) {
var remaining = '';
input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
var last = 0;
while (index > -1) {
var line = remaining.substring(last, index);
last = index + 1;
func(line);
index = remaining.indexOf('\n', last);
}
remaining = remaining.substring(last);
});
input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
});
}
为了将每行作为数组中的一项,在Node.js v18.11.0中添加了一个逐行读取文件的新函数
filehandle.readLines([选项])
这就是如何将此用于文本文件,您希望读取文件并将每行放入数组中
import { open } from 'node:fs/promises';
const arr = [];
myFilereader();
async function myFileReader() {
const file = await open('./TextFileName.txt');
for await (const line of file.readLines()) {
arr.push(line);
}
console.log(arr)
}
为了了解更多read Node.js文档,这里有文件系统readlines()的链接: https://nodejs.org/api/fs.html#filehandlereadlinesoptions
我只是想添加@finbarr伟大的答案,在异步的例子中的一个小修复:
异步:
var fs = require('fs');
fs.readFile('file.txt', function(err, data) {
if(err) throw err;
var array = data.toString().split("\n");
for(i in array) {
console.log(array[i]);
}
done();
});
@ madphysics, done()释放async。调用。