【问题标题】:Finding byte range correctly from nodejs readline从nodejs readline正确查找字节范围
【发布时间】: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


    【解决方案1】:

    我犯的范围计算错误是:

    你好世界 0 10 这是示例行。 11. 50

    换行时我没有添加一个。所以0-10是1,next不能是10到50,应该是11-50

    var lines = 0;
    var byterange = []
    var startingByte = 0;
    var totalBytesInThisBlock = 0;
    var lastByteLength = 0;
    var byteCursor = -1;
    var lastCursor = -1;
    
    rl.on('line', (input) => {
    //console.log(input);
    lines++;
    byteCursor++;
    lastByteLength = Buffer.byteLength(input);
    byteCursor += lastByteLength;
    //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(byteCursor-startingByte>400) {
        lastCursor = byteCursor;
        console.log("bytes="+startingByte+"-"+byteCursor);
        startingByte = byteCursor+1;
    //byterange.push("bytes="+startingByte+"-"+(lastByteLength+startingByte));
    //startingByte+=lastByteLength+1;
    //totalBytesInThisBlock=0;
    }
    });
    

    这里 byteCursor 完成了这项工作。

    【讨论】:

      猜你喜欢
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      相关资源
      最近更新 更多