Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何读取Node.js中的文件?_Javascript_Node.js_File_Filesystems_Fs - Fatal编程技术网

Javascript 如何读取Node.js中的文件?

Javascript 如何读取Node.js中的文件?,javascript,node.js,file,filesystems,fs,Javascript,Node.js,File,Filesystems,Fs,在Node.js中,我想读取一个文件,然后读取由\n分隔的文件的每一行。我如何才能做到这一点?试着阅读。请参考node.js中的API,也有一些类似的问题,因此,有一些问题可以尝试: 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){

在Node.js中,我想读取一个文件,然后读取由
\n
分隔的文件的每一行。我如何才能做到这一点?

试着阅读。

请参考node.js中的API,也有一些类似的问题,因此,有一些问题可以尝试:

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

在节点中读取文件的方法有很多。您可以在中了解所有这些

在您的例子中,假设您想要读取一个简单的文本文件,
countries.txt
,如下所示

Uruguay
Chile
Argentina
New Zealand
var fs = require('fs');
fs.readFile('countries.txt','utf8', function (err, data) {});
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');
  });
});
首先,您必须
require()
JavaScript文件顶部的
fs
模块,如下所示

Uruguay
Chile
Argentina
New Zealand
var fs = require('fs');
fs.readFile('countries.txt','utf8', function (err, data) {});
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');
  });
});
然后用它来读你的文件,你可以用这样的方法

Uruguay
Chile
Argentina
New Zealand
var fs = require('fs');
fs.readFile('countries.txt','utf8', function (err, data) {});
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');
  });
});
现在,在
{}
中,您可以与
readFile
方法的结果交互。如果出现错误,结果将存储在
err
变量中,否则,结果将存储在
数据
变量中。您可以在此处记录
数据
变量,以查看正在使用的内容

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
您还应该在第一次访问
数据之前,在行中添加
if(err)throw err
,说明读取文件时可能发生的任何错误。您可以将所有这些代码放在一个名为
read.js
的脚本中,如下所示

Uruguay
Chile
Argentina
New Zealand
var fs = require('fs');
fs.readFile('countries.txt','utf8', function (err, data) {});
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.txt
read.js
的目录,然后键入
node read.js
并按enter键。您应该可以在屏幕上看到注销的结果。祝贺您已使用节点读取了一个文件