0
Please help me in this question I'm not getting it
write a node.js code snippet to read the large data file (a text file available in the same directory) using streams, and logs the sizes/length of chunks of data read, to the console. I tried this one but the test cases are not getting passed var fs = require("fs"); var readStream = fs.createReadStream("dataFile.txt"); readStream .on("data", function(data) { var chunk = data.toString(); console.log(chunk.length); });
9 Réponses
+ 3
require path
+ 3
The question is a bit tricky but if you read it twice you will understand te question. Here's the solution:
const fs = require('fs');
let readerStream = fs.createReadStream('./Node-stream-handson/data_file.txt');
readerStream.setEncoding('UTF8');
readerStream.on('data',(chunk) => {
console.log(chunk.length);
});
+ 1
i think it's means how many bytes, like 8kb, 1mb, etc
0
@Gordon when I print the text on console then the data is printing
console.log(chunk);
but I did not understand what exactly the question is asking it's saying "logs the sizes/length of chunks of data read, to the console." here I will print the length only or anything else too
0
how can i get it using "chunk.length" or any other method
0
Did you solve this?Having same issue.
- 1
var fs = require("fs");
var readStream = fs.createReadStream("Node-stream-handson/data_file.txt");
readStream.on("data", function(data) {
var chunk = data.toString();
const fileSizeInBytes = chunk.length;
const fileSizeInMegabytes = fileSizeInBytes / 1000000.0;
console.log(fileSizeInMegabytes);
});
tried this one too
- 1
Here, the problem is with checking code logic developer has written. I will suggest to create new file by command
gt; touch App.js
and put following code into it
var fs = require("fs");
var readStream = fs.createReadStream("dataFile.txt");
readStream .on("data", function(data) {
var chunk = data.toString();
console.log(chunk.length);
});
It will work now Don't thanks me.