nodejs写入的文件无法换行

我用casperjs做了一个测试网页加载速度的demo,然后想通过fs.write把结果打印在一张txt上,我加入了换行符'n'但是打印出来的东西依旧不会换行,代码其中的一段类似这样:

var timeouttxt = '';
function timeOutTxt() {
    if (timend > 800) {
       timeouttxt = 'Page Times is ' + timend + ' is timeout 800\n';
    } else {
       timeouttxt = 'Page Times is ' + timend + '\n';
    }
}

txtData += 'the links length: ' + links.length + '\n' 
        + 'Page url is ' + this.getCurrentUrl() + '\n'
        + 'Page title is ' + this.getTitle() + ' index: ' + index + '\n'
        + timeouttxt
        + '-----------------------------------------------------\n';

fs.write(file, txtData, function(err) {
    if (err) return err;
});

输入出来的txt类似这样:

the links length: 23Page url is http://10.1.1.120/CRM/Page title is 卫安管理系统 index: 0----------------------------------------------------the links length: 23Page url ishttp://10.1.1.120/CRM/indexPage title is 卫安管理系统 index: 1-----------------------------------------------------the links length: 23Page url is http://10.1.1.120/CRM/updatePasswordPage title is 香港卫安管理系统 index: 2-----------------------------------------------------the links length: 23Page url is http://10.1.1.120/approve/Page title is 卫安管理系统 index: 3-----------------------------------------------------the links length: 23Page url is http://10.1.1.120/notice/Page title is  index: 4-----------------------------------------------------the links length: 23Page url is http://10.1.1.120/CRM/salesopportunities/indexPage title is 卫安管理系统 index: 5-----------------------------------------------------the links length: 23Page url ishttp://10.1.1.120/CRM/contact/homepagePage title is 卫安管理系统 index: 6

请教是什么原因?怎样换行?谢谢。

阅读 17.7k
1 个回答
\r 回车 \n 换行
windows \r\n linux \n mac \r
以前打字机一行结尾都要回车、换行。windows把这继承了下来,但是标志一行结尾用两个字符,有些浪费,所以Unix和mac就只用一个字符,linux继承Unix的。
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题