繁体   English   中英

节点fs.writefile \\ n不向新行添加文本

[英]Node fs.writefile \n not adding text to new line

我有一个日志文件,我正在添加文本。

这是代码:

function appendText(text) { 
    fs.writeFile('file.log', text+'\n',  {'flag':'a'}, (err) => {
        if (err) {
            return console.error(err);
        }
        console.log('Saved!');
    });
 }

用法:

appendText('some text here');

我的问题是它没有将文本添加到文件内容末尾的新行,而是将所有内容添加为单行。

我怎样才能解决这个问题?

使用appendfile()方法而不是像这样的writeFile()并使用\\ r \\ n而不是仅使用\\ n。

 const fs = require('fs');

function appendText(text) { 
fs.appendFile("test", `${text}\r\n`, function(err) {
  if(err) {
      return console.log(err);
  }

  console.log("The file was saved!");
});

}

appendText("hello");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM