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
1 Answer
Reset to default 3To 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条)