javascript - node.js fs.writeFileSync() How to set encoding to big5? - Stack Overflow

the fs.writeFileSync encode default is UTF 8I can't set the encode to big5.The documentation doe

the fs.writeFileSync encode default is UTF 8 I can't set the encode to big5. The documentation does not mention those encoding support. If this function does not support BIG5, what can I do?

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

When I set encoding(var encode='big5';) BIG5, the server generates an error.

the fs.writeFileSync encode default is UTF 8 I can't set the encode to big5. The documentation does not mention those encoding support. If this function does not support BIG5, what can I do?

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

When I set encoding(var encode='big5';) BIG5, the server generates an error.

Share Improve this question edited Jul 4, 2022 at 2:50 peteb 19.5k9 gold badges57 silver badges66 bronze badges asked Jul 19, 2016 at 4:30 FinnFinn 1,4596 gold badges25 silver badges50 bronze badges 1
  • Use something like iconv-lite. – robertklep Commented Jul 19, 2016 at 5:53
Add a ment  | 

1 Answer 1

Reset to default 3

To use an encoding that isn't standard with Node Core. You can use iconv-lite.

It adds support for additional encodings including big5, here is the full list of encodings.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745330040a4622834.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信