Nodejs reads garbled files

王林
Release: 2023-05-27 21:37:37
Original
859 people have browsed it

When reading files in Node.js, it is easy to encounter garbled characters. One common reason is that the file is not encoded in the correct format.

In Windows operating systems, the default text encoding format is GBK, but in most Unix and Linux operating systems, UTF-8 encoding format is used. Therefore, if a GBK-encoded file is opened in a Unix or Linux operating system, it will most likely produce garbled characters.

There are many ways to deal with the garbled problem during Node.js file reading. The following common methods are provided below:

  1. readFile() method

In Node.js, use the readFile() method of the fs module to read files . This method takes two parameters: file name and callback function. The callback function contains two parameters: the error object and the read data.

When reading a file, you can specify the encoding format of the file through the options parameter. For example, to specify the encoding as UTF-8, set the options parameter to {encoding: 'utf-8'}.

The following is an example of using the readFile() method to read a file:

const fs = require('fs');

fs.readFile('example.txt', { encoding: 'utf-8' }, (err, data) => {
    if (err) throw err;
    console.log(data);
});
Copy after login
  1. createReadStream() method

In addition to the readFile() method, you can also Use the createReadStream() method of the fs module to read the file. This method creates a Readable Stream that can read the file contents step by step.

The following is an example of using the createReadStream() method to read a file:

const fs = require('fs');

const readStream = fs.createReadStream('example.txt', { encoding: 'utf-8' });

readStream.on('data', (data) => {
    console.log(data);
});

readStream.on('error', (err) => {
    throw err;
});
Copy after login
  1. iconv-lite module

iconv-lite module is a module for A third-party module that handles text encoding in Node.js. It supports multiple encoding formats, including GBK, UTF-8, ISO-8859-1, and more.

The following is an example of using the iconv-lite module to read files:

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

const buffer = fs.readFileSync('example.txt');
const content = iconv.decode(buffer, 'gbk');

console.log(content);
Copy after login

The above method can help you deal with garbled characters during file reading in Node.js. Just choose the method that suits you based on your specific needs.

The above is the detailed content of Nodejs reads garbled files. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!