最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何fs.readFile(

IT培训 admin 5浏览 0评论

如何fs.readFile(

我们可以给一个字符串对Arg的fs.readFile代表文件路径

fs.readFile('/etc/passwd', (err, data) => {
  if (err) throw err;
  console.log(data);
});

但我注意到offical documentation说:

fs.readFile(path[, options], callback)

path可以是缓冲器或一个整数。然后我试着

const fs = require('fs')

fs.readFile(1, (err, data) => {
  if (err) throw err;
  console.log(data);
});

然后,它抛出一个错误。

我真糊涂,这ARG如何成为一个整数?任何人都可以给出一个例子吗?

回答如下:

你可以传递整数参数应该代表一个有效的文件描述符。例如,stdin通常文件描述符0,所以读stdin您可以使用此“文件”:

fs.readFile(0, (err, data) => {
  if (err) throw err;
  console.log(data);
});

而不是0,你也可以使用process.stdin.fd

得到一个文件描述符的另一种方法是使用fs.open()

如何fs.readFile(

我们可以给一个字符串对Arg的fs.readFile代表文件路径

fs.readFile('/etc/passwd', (err, data) => {
  if (err) throw err;
  console.log(data);
});

但我注意到offical documentation说:

fs.readFile(path[, options], callback)

path可以是缓冲器或一个整数。然后我试着

const fs = require('fs')

fs.readFile(1, (err, data) => {
  if (err) throw err;
  console.log(data);
});

然后,它抛出一个错误。

我真糊涂,这ARG如何成为一个整数?任何人都可以给出一个例子吗?

回答如下:

你可以传递整数参数应该代表一个有效的文件描述符。例如,stdin通常文件描述符0,所以读stdin您可以使用此“文件”:

fs.readFile(0, (err, data) => {
  if (err) throw err;
  console.log(data);
});

而不是0,你也可以使用process.stdin.fd

得到一个文件描述符的另一种方法是使用fs.open()

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论