开发者

How do I read a file in Node.js?

In Node.js, I want to read a file, and th开发者_JS百科en console.log() each line of the file separated by \n. How can I do that?


Try this:

var fs=require('fs');

fs.readFile('/path/to/file','utf8', function (err, data) {
  if (err) throw err;
  var arr=data.split('\n');
  arr.forEach(function(v){
    console.log(v);
  });
});


Try reading the fs module documentation.


Please refer to the File System API's in node.js, there is also few similar questions on SO, there is one of them


There are many ways to read a file in Node. You can learn about all of them in the Node documentation about the File System module, fs.

In your case, let's assume that you want to read a simple text file, countries.txt that looks like this;

Uruguay
Chile
Argentina
New Zealand

First you have to require() the fs module at the top of your JavaScript file, like this;

var fs = require('fs');

Then to read your file with it, you can use the fs.readFile() method, like this;

fs.readFile('countries.txt','utf8', function (err, data) {});

Now, inside the {}, you can interact with the results of the readFile method. If there was an error, the results will be stored in the err variable, otherwise, the results will be stored in the data variable. You can log the data variable here to see what you're working with;

fs.readFile('countries.txt','utf8', function (err, data) {
  console.log(data);
});

If you did this right, you should get the exact contents of the text file in your terminal;

Uruguay
Chile
Argentina
New Zealand

I think that's what you want. Your input was separated by newlines (\n), and the output will be as well since readFile doesn't change the contents of the file. If you want, you can make changes to the file before logging the results;

fs.readFile('calendar.txt','utf8', function (err, data) {
  // Split each line of the file into an array
  var lines=data.split('\n');

  // Log each line separately, including a newline
  lines.forEach(function(line){
    console.log(line, '\n');
  });
});

That will add an extra newline between each line;

Uruguay

Chile

Argentina

New Zealand

You should also account for any possible errors that happen while reading the file by adding if (err) throw err on the line right before you first access data. You can put all of that code together in a script called read.js like this;

var fs = require('fs');
fs.readFile('calendar.txt','utf8', function (err, data) {
  if (err) throw err;
  // Split each line of the file into an array
  var lines=data.split('\n');

  // Log each line separately, including a newline
  lines.forEach(function(line){
    console.log(line, '\n');
  });
});

You can then run that script in your Terminal. Navigate to the directory that contains both countries.txt and read.js, and then type node read.js and hit enter. You should see the results logged out on the screen. Congratulations! You've read a file with Node!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜