写入文本文件而不覆盖fs节点js [英] Write in a text file without overwriting in fs node js
问题描述
如何在文件中添加文字但不覆盖旧文本。我使用模块fs(节点js)
How I can add text in my file but without overwriting the old text. I use the module fs (node js)
我尝试了这段代码,但它不起作用。
I tried this code but it doesn't work.
fs.writeFileSync("file.txt", 'Text', "UTF-8",{'flags': 'w+'});
任何建议和谢谢。
推荐答案
检查此处的标志: http://nodejs.org/api /fs.html#fs_fs_open_path_flags_mode_callback - 您目前正在使用 w +
,其中:
Check the flags here: http://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback - you are currently using w+
which:
'w +' - 打开文件进行读写。创建文件(如果它不存在)或截断(如果存在)。
'w+' - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
你应该使用 a
而不是:
'a' - 打开要追加的文件。如果文件不存在,则创建该文件。
'a' - Open file for appending. The file is created if it does not exist.
'ax' - 与a类似,但以独占模式打开文件。
'ax' - Like 'a' but opens the file in exclusive mode.
'a +' - 打开文件进行阅读和追加。如果该文件不存在,则创建该文件。
'a+' - Open file for reading and appending. The file is created if it does not exist.
'ax +' - 类似于'a +'但以独占模式打开文件。
'ax+' - Like 'a+' but opens the file in exclusive mode.
这篇关于写入文本文件而不覆盖fs节点js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!