Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Node.js to read and write Chinese content files

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use Node.js to read and write Chinese content files". In daily operation, I believe many people have doubts about how to use Node.js to read and write Chinese content files. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Node.js to read and write Chinese content files". Next, please follow the editor to study!

Since Node.js only supports the following encodings: utf8, ucs2, ascii, binary, base64, hex, it does not support codes such as Chinese GBK or GB2312.

So if you want to read and write the Chinese contents of a file in GBK or GB2312 format, you must use an additional module: iconv-lite

1. Installation module: npm install iconv-lite

2. Demonstrate the code and copy the following code into a file, assuming it is ch.js (Note: the js file must be saved in utf8 encoding format):

/ / load File System read / write module var fs = require ('fs'); / / load transcoding module var iconv = require (' iconv-lite'); var file = "c:\\ a.txt"; writeFile (file); readFile (file); function writeFile (file) {/ / Test Chinese var str = "\ r\ n Hello myself!"; / / convert Chinese into byte array var arr = iconv.encode (str, 'gbk') Console.log (arr); / / appendFile, if the file does not exist, a new file is automatically created / / if writeFile is used, the old file is deleted and the new file fs.appendFile (file, arr, function (err) {if (file) console.log ("fail" + err); else console.log ("write file ok");}) } function readFile (file) {fs.readFile (file, function (err, data) {if (err) console.log ("read file fail" + err); else {/ / output byte array console.log (data) when read successfully; / / convert the array to gbk Chinese var str = iconv.decode (data, 'gbk'); console.log (str);}});}

3. Execute the js file with node.exe, and the result is as follows:

C:\ > node ch.js writes to the file ok I am a human Hello myself! C:\ >

Note the iconv module of 1:Node, which only supports linux and does not support Windows, so we should use pure js iconv-lite. Another: the author says that iconv-lite has better performance. For more information, please see git site: iconv-lite

Note 2: when I was testing to read and write files, I was always unable to write Chinese to the file. It was garbled and read normally. Later, my colleague helped me find that the encoding format of js file is that the code file of ansi,nodejs must be in utf8 format.

Note 3: if the files operated by the program are saved in UTF8 encoding format, then there is no need to use the iconv module to read the files directly in utf8 format, such as:

/ / the parameter file must be saved in utf8 format, otherwise the second parameter of function readFile (file) {/ / readFile indicates the read encoding format. If this parameter is not passed, the Buffer byte array fs.readFile (file, "utf8", function (err, data) {if (err) console.log ("read file fail" + err) is returned; console.log (data) when else {/ / read successfully / / output Chinese string directly});} at this point, the study on "how to use Node.js to read and write Chinese content files" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report