0

I don't know how this is possible new to programming. I want to print the values which are present in abc.txt, but don't know how to do it using Node.js:

abc.txt
[
mainfodler/subfolder/pqr.pdf, PQR
mainfodler/subfolder2/xyz.doc, XYZ
mainfodler/subfolder/image1.jpg, Image
mainfodler/subfolder2/pqr.pdf, PQR
]

Expected output:

Array1:
[
mainfodler/subfolder/pqr.pdf
mainfodler/subfolder2/xyz.doc
mainfodler/subfolder/image1.jpg
mainfodler/subfolder2/pqr.pdf
]

Array2
[
PQR
XYZ
Image
PQR
]

wuerfelfreak
  • 2,363
  • 1
  • 14
  • 29
  • Does this answer your question? [Read a text file using Node.js?](https://stackoverflow.com/questions/9168737/read-a-text-file-using-node-js) – rook218 Apr 13 '21 at 12:35
  • I don't think so this answers the question. The expected o/p is segregated in two parts Array1 and Array2. It is just reading the whole file in one go. – Apoorva Chikara Apr 13 '21 at 16:30

2 Answers2

0

This is a basic example with the fs library. You can do something similar to the following:

const fs = require('fs');

fs.readFile('my-file.txt', 'utf8', function(err, data) {
    if (err) throw err;
    console.log(data);
});

Learn more with the docs here

  • I don't think so this answers the question. The expected o/p is segregated in two parts Array1 and Array2. It is just reading the whole file in one go. – Apoorva Chikara Apr 13 '21 at 16:22
0

You can use readline from node.js. Here is the code you can use:

const readline = require('readline');
const fs = require('fs');

async function readFile(filePath) {   
const myInterface = readline.createInterface({ input: fs.createReadStream(filePath)});    
  const arrayLink = [],     
        arrayName = []; 

  for await (const line of myInterface) {
    let temp = line.split(",");
    arrayLink.push(temp[0]);     
    arrayName.push(temp[1]);     
  }
  console.log(arrayLink, arrayName);
}
readFile(filePath)

This Updated code will run the way you are expecting. Check this one.

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35
  • Hello, I am writing this code in a function but when i am printing array by returning the array its showing me empty [ ] `function readFile(filePath) { const myInterface = readline.createInterface({ input: fs.createReadStream(filePath), }); const arrayLink = [], arrayName = []; myInterface.on("line", function (line) { // console.log(line.split(",")); let temp = line.split(","); arrayLink.push(temp[0]); arrayName.push(temp[1]); return [arrayLink, arrayName]; }); console.log(arrayLink, arrayName); }` @apoorva –  Apr 13 '21 at 17:19
  • I have updated the image for the running code. – Apoorva Chikara Apr 13 '21 at 17:27
  • You need to understand, `console.log(arrayLink, arrayName);` will be called first and it will always print empty data. and you can't return data like this `return [arrayLink, arrayName]; ` from an array instead you should wait for the result to read first and then get it. – Apoorva Chikara Apr 13 '21 at 17:29
  • its showing me console in myInterface.on() but not showing oustide what should i do and i wrote by mistakenly `return [ arrayLink, arrayName]` –  Apr 13 '21 at 17:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231075/discussion-between-apoorva-chikara-and-praveen). – Apoorva Chikara Apr 13 '21 at 17:37
  • 1
    Check the updated code. Try to run the same code and pass the file path. – Apoorva Chikara Apr 13 '21 at 17:39
  • 1
    Yes sorry for delay thank you and its working absolutely fine :) @apoorva –  Apr 13 '21 at 18:09
  • 1
    It's ok don't be sorry about it. We all are here to help each other. – Apoorva Chikara Apr 13 '21 at 18:10