JS:为什么fs.readFileSync()返回缓冲区,即使我之前使用toString()将其转换为字符串?

问题描述 投票:-1回答:1

希望你很好。

简而言之:我的目标是读取我通过对话框提取的文件。 filepath存储在名为files的对象中。可以使用键files.filePaths提取路径。我想读取该文件(这是一个简单的文本文件)的输入,并将其转换为字符串。

const content = fs.readFileSync(files.filePaths.toString())      // here i converted it into a string
console.log(content)   // however, returns a Buffer Object

console.log(content.toString())       // This however returns the string.

有人知道为什么在这种情况下我必须两次使用toString()吗?

非常感谢。

最佳团员

javascript tostring fs
1个回答
0
投票

您传递到readFileSync的文件path是与readFileSync的返回值不同的值(除非您提供编码选项,否则它是一个缓冲区,除非(作为the documentation says)。]]

© www.soinside.com 2019 - 2024. All rights reserved.