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.