gpt4 book ai didi

javascript - 写入文本文件而不覆盖 fs Node js

转载 作者:IT老高 更新时间:2023-10-28 23:07:34 34 4
gpt4 key购买 nike

如何在文件中添加文本但不覆盖旧文本。我使用模块 fs (node js)

我试过这段代码,但它不起作用。

fs.writeFileSync("file.txt", 'Text', "UTF-8",{'flags': 'w+'});

任何建议,谢谢。

最佳答案

在此处检查标志:http://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback - 你目前正在使用 w+ 其中:

'w+' - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).

你应该使用 a 代替:

'a' - Open file for appending. The file is created if it does not exist.

'ax' - Like 'a' but opens the file in exclusive mode.

'a+' - Open file for reading and appending. The file is created if it does not exist.

'ax+' - Like 'a+' but opens the file in exclusive mode.

关于javascript - 写入文本文件而不覆盖 fs Node js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17324797/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com