Home > Article > Web Front-end > What should I do if node reads garbled files?
Solution to the garbled file read by node: 1. Enter the command "npm install iconv-lite" in the terminal; 2. Create the main.js file and add "iconv.decode(buf,'GBK') ;"; 3. Run the main.js file in the terminal.
The operating environment of this article: Windows 7 system, nodejs version 10.16.2, Dell G3 computer.
What should I do if node reads garbled files?
Node.js reads Chinese garbled files and solves them:
Use vsCode Open the project folder, open the terminal (Ctrl ~), install iconv-lite locally, enter the command in the terminal:
npm install iconv-lite
After the installation is complete , you can test it: create the input.txt file, write Chinese in it, and create the main.js file.
main.js file content:
var iconv = require('iconv-lite'); var fs = require('fs'); var fileStr = fs.readFileSync('input.txt',{encoding:'binary'}); var buf = new Buffer(fileStr,'binary'); var str = iconv.decode(buf,'GBK'); console.log(str);
Run the main.js file in the terminal:
node main.js
The console will print out the text information in input.txt and the display will be normal!
Recommended learning: "node.js Video Tutorial"
The above is the detailed content of What should I do if node reads garbled files?. For more information, please follow other related articles on the PHP Chinese website!