希望你很好。
简而言之:我的目标是读取我通过对话框提取的文件。 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()吗?
非常感谢。
最佳团员
您传递到readFileSync
的文件path是与readFileSync
的返回值不同的值(除非您提供编码选项,否则它是一个缓冲区,除非(作为the documentation says)。]]