我可以问一下,在JavaScript中有没有类似于file_put_contents的等效函数?
提前致谢。
我可以问一下,在JavaScript中有没有类似于file_put_contents的等效函数?
提前致谢。
在JavaScript中?不行。
在NodeJS中?可以,它被称为writeFile
。以下是文档中的示例:
fs.writeFile('message.txt', 'Hello Node', function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
如果您不想覆盖文件的内容,而是想将内容追加到文件中,请参考上面的回复。
var fs = require('fs');
fs.writeFile(
"foo.txt", // file
"bar", // string
{
encoding: "utf8",
flag: "a" // flag that specifies that it will append stuff
},
function(){ console.log("done!") }
)