【问题标题】:NodeJS readFile On WindowsWindows 上的 NodeJS 读取文件
【发布时间】:2015-09-23 06:37:37
【问题描述】:

我一直遇到 Windows 上的编码问题。

var content = fs.readFileSync("file.txt", 'utf8').toString();
console.log(content)

file.txt 是在记事本中使用文本“123”创建的。上面的输出是

??1 2 3

而不是我期望的“123”。如果我在 Windows 上阅读在 Mac 上创建的文本文件,它工作正常。所以只是 Windows 创建的文件导致了我的问题。

我在网络上打猎但没有运气,包括这篇文章node.js readfile error with utf8 encoded file on windows

我还尝试了 toString('utf8'),但没有成功。

稍后在代码中,我使用此输入构造了一个 JSON 字符串,结果为

??1\u00002\u00003\u0000�

哎呀。

我做错了什么?

谢谢 加里。

【问题讨论】:

标签: node.js encoding


【解决方案1】:

您链接到的问题有您想要的答案。记事本使用byte-order mark (BOM) 以2 字节编码保存文件。看看this question的答案。

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2018-08-25
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多