首先,您需要搜索行的起点和终点。接下来,您需要使用一个函数来替换该行。我使用我的一个库来解决第一部分的问题: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 ();
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)