Lar*_*tle 8 javascript filesystems node.js
使用node.js覆盖大型(2MB +)文本文件中的行的最佳方法是什么?
我目前的方法涉及到
\n
)将缓冲区拆分为数组.\n
.首先,您需要搜索该行的起点和终点。接下来,您需要使用一个函数来替换该行。我使用我的一个库找到了第一部分的解决方案:Node-BufferedReader。
var lineToReplace = "your_line_to_replace";
var startLineOffset = 0;
var endLineOffset = 0;
new BufferedReader ("your_file", { encoding: "utf8" })
.on ("error", function (error){
console.log (error);
})
.on ("line", function (line, byteOffset){
startLineOffset = endLineOffset;
endLineOffset = byteOffset - 1; //byteOffset is the offset of the NEXT byte. -1 if it's the end of the file, if that's the case, endLineOffset = <the file size>
if (line === lineToReplace ){
console.log ("start: " + startLineOffset + ", end: " + endLineOffset +
", length: " + (endLineOffset - startLineOffset));
this.interrupt (); //interrupts the reading and finishes
}
})
.read ();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4229 次 |
最近记录: |