NodeJs - 如何使用 BOM 编写函数 fs.writeFile?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13859218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NodeJs - how to make function fs.writeFile write with BOM?
提问by user706355
I'm using nodeJS v0.8.6 and the native library fs. Here is my code :
我正在使用 nodeJS v0.8.6 和本机库 fs。这是我的代码:
var filesys = require('fs');
filesys.writeFile('test.txt', 'This is an example with accents : é è à ','utf8', function (err) {});
The problem is that it writes in utf8 without BOM (I use notepad++ to verify it) and it doesn't work in wordpad on Windows (the accents are not well displayed). The thing is that I need that file to be well read by womeone using wordpad.
问题是它在没有 BOM 的情况下用 utf8 写入(我使用 notepad++ 来验证它)并且它在 Windows 上的写字板中不起作用(重音显示不正确)。问题是我需要使用写字板的女性能够很好地阅读该文件。
How can I add the BOM to my file ?
如何将 BOM 添加到我的文件中?
回答by Esailija
UTF-8 doesn't require a bom, but you can add it by yourself of course.
UTF-8 不需要 bom,但您当然可以自行添加。
filesys.writeFile('test.txt', '\ufeffThis is an example with accents : é è à ','utf8', function (err) {});
回答by Jeff Fischer
I elaborated on this answer in detail on this answer - Adding UTF-8 BOM to string/Blob.
我在这个答案中详细阐述了这个答案 -将 UTF-8 BOM 添加到 string/Blob。
This is a very sparse answer that doesn't go into detail as to whythis works. The FEFF bytes are actually the UTF16LE BOM, so the previous answer is confusing.
这是一个非常稀疏的答案,没有详细说明为什么会这样。FEFF 字节实际上是 UTF16LE BOM,因此之前的答案令人困惑。

