DaB*_*ars 80 javascript newline node.js
我正在尝试使用Node.js将数据附加到日志文件,并且工作正常,但它不会进入下一行. \n
似乎没有在我的功能下面工作.有什么建议?
function processInput ( text )
{
fs.open('H://log.txt', 'a', 666, function( e, id ) {
fs.write( id, text + "\n", null, 'utf8', function(){
fs.close(id, function(){
console.log('file is updated');
});
});
});
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*ska 139
看起来你在Windows上运行它(给定你的H://log.txt
文件路径).
尝试使用\r\n
而不仅仅是\n
.
老实说,\n
很好; 您可能正在查看记事本中的日志文件或其他不呈现非Windows换行符的日志文件.尝试在不同的查看器/编辑器(例如写字板)中打开它.
小智 73
请改用os.EOL常量.
var os = require("os");
function processInput ( text )
{
fs.open('H://log.txt', 'a', 666, function( e, id ) {
fs.write( id, text + os.EOL, null, 'utf8', function(){
fs.close(id, function(){
console.log('file is updated');
});
});
});
}
Run Code Online (Sandbox Code Playgroud)
使用\r\n
组合在节点 js 中追加新行
var stream = fs.createWriteStream("udp-stream.log", {'flags': 'a'});
stream.once('open', function(fd) {
stream.write(msg+"\r\n");
});
Run Code Online (Sandbox Code Playgroud)
小智 5
或者,您可以使用fs.appendFile方法
let content = 'some text';
content += "\n";
fs.appendFile("helloworld.txt", content, (err) => {
return console.log(err);
});
Run Code Online (Sandbox Code Playgroud)