2

fs.writeFileSync编码默认为UTF 8 我无法将编码设置为 big5 。该文档没有提到那些编码支持。如果这个功能不支持BIG5,我该怎么办?

var fs = require('fs');
var FilePath='./text.txt';
var Str='this is a test!';
var encode='utf8';
fs.writeFileSync(FilePath, Str, encode);

当我设置 encoding(var encode='big5';) BIG5 时,服务器会产生错误。

4

1 回答 1

3

使用不符合 Node Core 标准的编码。您可以使用iconv-lite

它增加了对其他编码的支持,包括big5这里是编码的完整列表

const iconv = require('iconv-lite');
const fs = require('fs');
const stream = require('stream');

var Str = iconv.encode('This is a test', 'big5');

var readStream = new stream.PassThrough();
var writeStream = fs.createWriteStream('./text.txt');

readStream.once('error', (err) => { console.log(err); });    
readStream.once('end', () => { console.log('File Written'); });

readStream.end(Str); // write data to stream    
readStream.pipe(writeStream); // pipe data to file
于 2016-07-19T05:53:52.363 回答