node 读取文件 文件夹_使用Node读取文件

在Node.js中,可以使用fs.readFile()或fs.readFileSync()方法读取文件,它们默认以utf8编码读取文件。对于大文件,推荐使用流来避免内存占用过多。若要读取文件夹,需要结合fs模块的其他方法如fs.readdir()。
摘要由CSDN通过智能技术生成

node 读取文件 文件夹

The simplest way to read a file in Node is to use the fs.readFile() method, passing it the file path and a callback function that will be called with the file data (and the error):

在Node中读取文件的最简单方法是使用fs.readFile()方法, fs.readFile()传递文件路径和将与文件数据一起调用的回调函数(以及错误):

const fs = require('fs')

fs.readFile('/Users/flavio/test.txt', (err, data) => {
  if (err) {
    console.error(err)
    return
  }
  console.log(data)
})

Alternatively, you can use the synchronous version fs.readFileSync():

另外,您可以使用同步版本fs.readFileSync()

const fs = require('fs')

try {
  const data = fs.readFileSync('/Users/flavio/test.txt', 'utf8')
  console.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值