javascript - nodejs写入的文件无法换行
怪我咯
怪我咯 2017-04-11 12:08:44
[JavaScript讨论组]

我用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

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

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
迷茫
\r 回车 \n 换行
windows \r\n linux \n mac \r
以前打字机一行结尾都要回车、换行。windows把这继承了下来,但是标志一行结尾用两个字符,有些浪费,所以Unix和mac就只用一个字符,linux继承Unix的。
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号