The fs.read () method of nodejs reads part of the file, causing the last Chinese character to read only one bit.

use the fs.read () method of nodejs to read part of the txt file. The
buffer is set to 1024 bytes.
because one Chinese character occupies two bytes, occasionally the last Chinese character reads only one byte, resulting in garbled code

.
// 

var fs = require("fs")

// 10240
var buf = new Buffer.alloc(1024,"utf8");

fs.open("text.txt", "r+", function(err,fd){
    if(err) throw err
    console.log("")
    fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){
        if(err) throw err
        console.log(bytes + "  ");
        // 
        if(bytes > 0){
            console.log(":" + buf.slice(0, bytes).toString());
        }
    })
})
Jul.11,2022

it is recommended to use iconv-lite . Here is an example:

const fs = require('fs');
const res = fs.createReadStream('data.txt', { highWaterMark: 1024});
const iconv = require('iconv-lite');

let chunks = [];
let size = 0;
res.on('data', function (chunk) {
  chunks.push(chunk);
  size += chunk.length;
});
res.on('end', function () {
  let buf = Buffer.concat(chunks, size);
  let str = iconv.decode(buf, 'utf8');
  // 
  console.log(str);
});

if you can make sure that the file you read is only UTF-8 encoded, you don't have to introduce a third-party module:

const fs = require('fs');
const res = fs.createReadStream('data.txt', { highWaterMark: 1024});
res.setEncoding('utf8');

let data = '';
res.on('data', function (chunk){
  data += chunk;
});
res.on('end', function () {
  // 
  console.log(data);
});

utf8 A Chinese character with three bytes

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e698ea-44cb4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e698ea-44cb4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?