javascript - nodejs写入的文件无法换行

查看:599
本文介绍了javascript - 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

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

解决方案

\r 回车 \n 换行
windows \r\n linux \n mac \r
以前打字机一行结尾都要回车、换行。windows把这继承了下来,但是标志一行结尾用两个字符,有些浪费,所以Unix和mac就只用一个字符,linux继承Unix的。

这篇关于javascript - nodejs写入的文件无法换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆