【发布时间】:2021-01-05 15:06:18
【问题描述】:
我正在使用 node js 的 readline 读取一个大的 csv 文件。我想找出我正确读取了多少字节。或块的字节位置。 readline 正在计算第一行的换行符,但我猜其他人没有,所以这有点使整个字节范围错误。有人可以帮忙吗?另外,告诉这是否是正确的方法? 另外,假设我读完后可以知道字节位置是什么,比如说 100 行。
const fs = require("fs");
const readline = require("readline");
const csvfile = "csv-file.csv";
// stream
let stream1 = fs.createReadStream(csvfile);
// Stream 2 to read only the specific bytes
//let stream2 = fs.createReadStream(csvfile, { start: 97, end: 174 });
let rl = readline.createInterface({
input: stream1,
terminal: false,
});
var lines = 0;
var byterange = []
var startingByte = 0;
var totalBytesInThisBlock = 0;
// Print those specific bytes and see if it's working as expected.
// stream2.on('data', (data) => {
// console.log(data.toString('utf8'));
// });
// stream2.on('end', ()=>{});
rl.on('line', (input) => {
//console.log(input);
lines++;
totalBytesInThisBlock += Buffer.byteLength(input);
console.log("total bytes till line "+lines +" and adding "+Buffer.byteLength(input)+" now : " + totalBytesInThisBlock);
//Making blocks of 4 lines i.e. starting byte of 1st line starting byte to 4th line ending byte
// 5th line starting byte to ending byte of 8th line and so on.
if(lines%4==0) {
byterange.push("bytes="+startingByte+"-"+(totalBytesInThisBlock+startingByte));
startingByte+=totalBytesInThisBlock+1;
totalBytesInThisBlock=0;
}
});
rl.on('close', () => {
console.log("lines read : " + lines);
console.log(byterange);
//Total bytes in file
var filedata = fs.readFileSync(csvfile);
console.log(Buffer.byteLength(filedata));
});
【问题讨论】:
标签: javascript node.js readline