admin管理员组文章数量:1356588
I tried to read line by line in file, but I have a doubt how to print count of line in file using nodejs
.
data.js
console.log("123")
console.log("123")
console.log("123")
console.log("123")
file.js
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('./data.js')
});
lineReader.on('line', function (line) {
console.log('Line from file:', line);
});
I got this ouput
Line from file: console.log("123") Line from file:
Line from file: console.log("123") Line from file:
Line from file: Line from file: console.log("123") Line from file: Line from file: Line from file: Line from file: console.log("123")
but I want how many lines of code in file using node js
I tried to read line by line in file, but I have a doubt how to print count of line in file using nodejs
.
data.js
console.log("123")
console.log("123")
console.log("123")
console.log("123")
file.js
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('./data.js')
});
lineReader.on('line', function (line) {
console.log('Line from file:', line);
});
I got this ouput
Line from file: console.log("123") Line from file:
Line from file: console.log("123") Line from file:
Line from file: Line from file: console.log("123") Line from file: Line from file: Line from file: Line from file: console.log("123")
but I want how many lines of code in file using node js
Share Improve this question edited May 14, 2019 at 5:53 SuperStar518 2,9052 gold badges21 silver badges37 bronze badges asked May 14, 2019 at 5:32 smith harismith hari 4371 gold badge11 silver badges23 bronze badges 1- stackoverflow./questions/12453057/… This can help you – abby37 Commented May 14, 2019 at 5:35
3 Answers
Reset to default 4const fs = require('fs')
fs.readFile('source', 'utf8', (err, data) => {
console.log(data.split('\n').length)
})
First of all import fs library, then, read file and get length by splitting data
var i;
var count = 0;
require('fs').createReadStream(process.argv[2])
.on('data', function(chunk) {
for (i=0; i < chunk.length; ++i)
if (chunk[i] == 10) count++;
})
.on('end', function() {
console.log(count);
});
let count = 0;
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('./data.js')
});
lineReader.on('data', line => {
for (i=0; i < line.length; ++i) if (line[i] == 10) count++;
})
.on('end', () => {
console.log(count);
})
By looping the lines in file you count the number of lines this way. Also you can checkout this link for more details
本文标签: javascripthow to check how many lines of code in file using node jsStack Overflow
版权声明:本文标题:javascript - how to check how many lines of code in file using node js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744066975a2585181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论