[Tips]Javascrip计算文件行数
2024-09-26 23:39:26
function calcLineCount(filename, callback) {
var fs = require('fs'); var fileStream = fs.createReadStream(filename),
lines = 0; fileStream.on('data', function (buf) {
lines += buf.toString().match(/\n/g).length + 1;
}); fileStream.on('end', function () {
callback(lines);
});
} calcLineCount('/Users/piaoger/Downloads/lc.js', function(lines) {
console.log(lines);
});
其实也可以直接在Node.js中调用“wc”来解决问题的。只不过我之前用str.split(/\n/).length来做,应该会比match慢吧。
Piaoger
最新文章
- Linux学习之CentOS(二十二)--单用户模式下修改Root用户的密码
- Python网络socket学习
- iOS常用第三方
- 打印出所有的 ";水仙花数 ";,所谓 ";水仙花数 ";是指一个三位数 其各位数字立方和等于该数本身。 例如:153是一个 ";水仙花数 "; 因为153=1*1*1+5*5*5+3*3*3
- 金蝶EAS常用表
- MVC项目实践,在三层架构下实现SportsStore-08,部署到IIS服务器
- List,set,Map 的用法和区别
- Swift学习初步(一)
- System Generator入门笔记
- 在ubuntu系统荣品开发配套JDK安装
- CSS+Javascript的那些框架
- iOS7——图像资源Images Assets
- 2014.8.20break,continue,字符串,数字和日期
- vscode: Visual Studio Code 常用快捷键
- SQLite基础回顾
- oracle 常用知识积累
- SQL 高效运行注意事项(二)
- Git冲突:You have not concluded your merge
- Django之验证码
- composer 出现You are running Composer with SSL/TLS protection disabled.
热门文章
- RocketMQ 拉取消息-文件获取
- PHP的抽象类、接口的区别和选择[转载]
- Python strings, 元组tuples, 和numbers是不可更改的对象,而list,dict等则是可以修改的
- JMeter 五:监控服务器
- Android常用酷炫控件(开源项目)github地址汇总
- 基于Prometheus搭建SpringCloud全方位立体监控体系
- github 管理图示
- 08-session详解
- java数据库操作:JDBC的操作
- 项目启动报错:No suitable driver found for jdbc:oracle:thin:@192.168.7.146:1521:oracle