Phantomjs append to file with fs.write

后端 未结 1 1619
长发绾君心
长发绾君心 2021-02-19 06:19

How can I append to a file using fs.write()?

Using fs.write on the same files overwrites the content:

var fs = require(\'fs\');
try {
    fs         


        
1条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-19 06:41

    Use append mode a instead of [over]write mode w in the fs.write call.

    var fs = require('fs');
    try {
        fs.write("file.txt", "Hello World", 'a');
        fs.write("file.txt", "Hello World", 'a');
    } catch(e) {
        console.log(e);
    }
    

    I inferred this based on the python open() C fopen documentation; Glad it worked, other file modes may work but were not tested by me.

    0 讨论(0)
提交回复
热议问题