Phantomjs append to file with fs.write

这一生的挚爱 提交于 2019-12-05 14:53:12

问题


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.write("file.txt", "Hello World", 'w');
    fs.write("file.txt", "Hello World", 'w');
} catch(e) {
    console.log(e);
}

回答1:


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.



来源:https://stackoverflow.com/questions/25631963/phantomjs-append-to-file-with-fs-write

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!