本文介绍了Phantom.js用fs.write附加到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用fs.write()附加到文件?

在相同文件上使用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);
}

推荐答案

在fs.write调用中,使用附加模式a代替[over] write模式w.

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);
}

我是根据 C fopen 文档;很高兴,其他文件模式也可能有效,但未经我测试.

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

这篇关于Phantom.js用fs.write附加到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 07:55