How to append to a file in Node?

后端 未结 18 1179
庸人自扰
庸人自扰 2020-11-22 09:23

I am trying to append a string to a log file. However writeFile will erase the content each time before writing the string.

fs.writeFile(\'log.txt\'         


        
相关标签:
18条回答
  • 2020-11-22 10:17

    I offer this suggestion only because control over open flags is sometimes useful, for example, you may want to truncate it an existing file first and then append a series of writes to it - in which case use the 'w' flag when opening the file and don't close it until all the writes are done. Of course appendFile may be what you're after :-)

      fs.open('log.txt', 'a', function(err, log) {
        if (err) throw err;
        fs.writeFile(log, 'Hello Node', function (err) {
          if (err) throw err;
          fs.close(log, function(err) {
            if (err) throw err;
            console.log('It\'s saved!');
          });
        });
      });
    
    0 讨论(0)
  • 2020-11-22 10:20

    Using jfile package :

    myFile.text+='\nThis is new line to be appended'; //myFile=new JFile(path);
    
    0 讨论(0)
  • 2020-11-22 10:20

    Here's a full script. Fill in your file names and run it and it should work! Here's a video tutorial on the logic behind the script.

    var fs = require('fs');
    
    function ReadAppend(file, appendFile){
      fs.readFile(appendFile, function (err, data) {
        if (err) throw err;
        console.log('File was read');
    
        fs.appendFile(file, data, function (err) {
          if (err) throw err;
          console.log('The "data to append" was appended to file!');
    
        });
      });
    }
    // edit this with your file names
    file = 'name_of_main_file.csv';
    appendFile = 'name_of_second_file_to_combine.csv';
    ReadAppend(file, appendFile);
    
    0 讨论(0)
  • 2020-11-22 10:22

    You need to open it, then write to it.

    var fs = require('fs'), str = 'string to append to file';
    fs.open('filepath', 'a', 666, function( e, id ) {
      fs.write( id, 'string to append to file', null, 'utf8', function(){
        fs.close(id, function(){
          console.log('file closed');
        });
      });
    });
    

    Here's a few links that will help explain the parameters

    open
    write
    close


    EDIT: This answer is no longer valid, look into the new fs.appendFile method for appending.

    0 讨论(0)
  • 2020-11-22 10:22

    Try to use flags: 'a' to append data to a file

     var stream = fs.createWriteStream("udp-stream.log", {'flags': 'a'});
      stream.once('open', function(fd) {
        stream.write(msg+"\r\n");
      });
    
    0 讨论(0)
  • 2020-11-22 10:24

    Your code using createWriteStream creates a file descriptor for every write. log.end is better because it asks node to close immediately after the write.

    var fs = require('fs');
    var logStream = fs.createWriteStream('log.txt', {flags: 'a'});
    // use {flags: 'a'} to append and {flags: 'w'} to erase and write a new file
    logStream.write('Initial line...');
    logStream.end('this is the end line');
    
    0 讨论(0)
提交回复
热议问题