【问题标题】:How do I read a file in Node.js?如何在 Node.js 中读取文件?
【发布时间】:2011-11-05 16:46:16
【问题描述】:

在Node.js中,我想读取一个文件,然后console.log()文件的每一行用\n分隔。我该怎么做?

【问题讨论】:

    标签: javascript node.js file filesystems fs


    【解决方案1】:

    试试这个:

    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);
      });
    });
    

    【讨论】:

      【解决方案2】:

      尝试阅读fs module documentation

      【讨论】:

        【解决方案3】:

        请参考node.js中File System的API,SO上类似的问题也很少,有one of them

        【讨论】:

          【解决方案4】:

          在 Node.js 中有很多方法可以读取文件。你可以在the Node documentation about the File System module, fs了解所有这些。

          在您的情况下,假设您要读取一个简单的文本文件,countries.txt,看起来像这样;

          Uruguay
          Chile
          Argentina
          New Zealand
          

          首先你必须在 JavaScript 文件的顶部 require() fs 模块,像这样;

          var fs = require('fs');
          

          然后用它来读取你的文件,你可以使用fs.readFile()方法,像这样;

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

          现在,在{} 中,您可以与readFile 方法的结果进行交互。如果发生错误,结果将存储在err 变量中,否则,结果将存储在data 变量中。您可以在此处记录data 变量以查看您正在使用的内容;

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

          如果你做对了,你应该在终端中获得文本文件的确切内容;

          Uruguay
          Chile
          Argentina
          New Zealand
          

          我想这就是你想要的。您的输入由换行符 (\n) 分隔,并且输出也会如此,因为 readFile 不会更改文件的内容。如果需要,您可以在记录结果之前对文件进行更改;

          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');
            });
          });
          

          这将在每行之间添加一个额外的换行符;

          Uruguay
          
          Chile
          
          Argentina
          
          New Zealand
          

          您还应该通过在首次访问data 之前在该行添加if (err) throw err 来解决读取文件时可能发生的任何错误。您可以像这样将所有这些代码放在一个名为 read.js 的脚本中;

          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');
            });
          });
          

          然后您可以在终端中运行该脚本。导航到包含countries.txtread.js 的目录,然后键入node read.js 并按Enter。您应该会在屏幕上看到已注销的结果。恭喜!你已经用 Node 读取了一个文件!

          【讨论】:

            猜你喜欢
            • 2013-08-25
            • 2015-08-23
            • 1970-01-01
            • 2015-12-28
            • 2021-11-29
            • 1970-01-01
            • 2013-11-23
            • 2018-05-04
            • 1970-01-01
            相关资源
            最近更新 更多