简体   繁体   English

如何使用节点js检查文件中的几行代码

[英]how to check 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 . 我试图逐行读取文件中的内容,但是我nodejs如何使用nodejs打印文件中的行数。

data.js data.js

console.log("123")

console.log("123")


console.log("123")



console.log("123")

file.js 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: 来自文件的行:console.log(“ 123”)来自文件的行:
Line from file: console.log("123") Line from file: 来自文件的行:console.log(“ 123”)来自文件的行:
Line from file: Line from file: console.log("123") Line from file: Line from file: Line from file: Line from file: console.log("123") 来自文件的行:来自文件的行:console.log(“ 123”)来自文件的行:来自文件的行:来自文件的行:来自文件的行:console.log(“ 123”)

but I want how many lines of code in file using node js 但是我想要使用节点js文件中多少行代码

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 您也可以查看此链接以获取更多详细信息

const 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 首先导入fs库,然后读取文件并通过拆分数据来获取长度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Node JS在api中检查用户名和密码的几种方法? - How many ways to check username and password in api using node js? 如何使用 node.js 检查文本文件中的重复行? - How can I check for repeating lines in a text file with node.js? 如何使用 js 或 node 检查图像尺寸 - How to check an image dimensions using js or node 使用JQuery而不是直接JavaScript保存多少行代码? - How many lines of code will be saved using JQuery instead of straight JavaScript? 如何在Node JS中同步读取文本文件和将行分组在一起 - How to read a text file and group lines together in node js synchronously 如何使用ESLint检查js文件的最大行数? - How to check max count of lines of a js file with ESLint? 检查多少行 <p> 标签占据 - Check how many lines will a <p> tag occupy 我如何使用node.js函数检查我的VM实例在google-cloud中使用了多少CPU和内存 - How can I check how many CPUs and memory my vm instance uses in google-cloud using node.js function 节点js - 如何在等待节点js中的用户输入时暂停后续代码行的执行 - Node js - How to pause execution of subsequent lines of code while waiting for user input in node js 如何使用Promise和Node.js正确检查和记录HTTP状态代码? - How to properly check and log http status code using promises and node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM