Overwrite a line in a file using node.js

What's the best way to overwrite a line in a large (2MB+) text file using node.js?

My current method involves

  • copying the entire file into a buffer.
  • Spliting the buffer into an array by the new line character ( n ).
  • Overwriting the line by using the buffer index.
  • Then overwriting the file with the buffer after join with n .

  • First, you need to search where the line starts and where it ends. Next you need to use a function for replacing the line. I have the solution for the first part using one of my libraries: 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 ();
    
    链接地址: http://www.djcxy.com/p/11036.html

    上一篇: 为什么ICollection不包含Add方法?

    下一篇: 使用node.js覆盖文件中的一行