亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home WeChat Applet Mini Program Development Using Node.js to convert file encoding formats

Using Node.js to convert file encoding formats

Jun 04, 2018 pm 03:05 PM
javascript node.js coding

這次給大家?guī)砝?a href="http://ipnx.cn/wiki/1498.html" target="_blank">Node.js進(jìn)行文件編碼格式轉(zhuǎn)換,利用Node.js進(jìn)行文件編碼格式轉(zhuǎn)換的注意事項(xiàng)有哪些,下面就是實(shí)戰(zhàn)案例,一起來看一下。

ASCII編碼就比較蛋疼,通過搜索網(wǎng)上資源,反復(fù)測(cè)試對(duì)比,最終形成下面比較靠譜的方法(有一些 EditPlus顯示編碼為utf-8但node.js庫(kù)返回的卻是其它編碼>_<)

判斷修改是否無(wú)誤,只需要在修改完之后,通過SVN提交,瀏覽提交列表,雙擊任意一項(xiàng)待提交文件,如果顯示下圖所示的對(duì)話框,則說明修改成功,其它都會(huì)看到中文反而變成亂碼了

var fs = require('fs');
var chardet = require('chardet');
var jschardet = require("jschardet");
var encoding = require("encoding");
var path = "lua目錄";
function readDirectory(dirPath) {
  if (fs.existsSync(dirPath)) {
    var files = fs.readdirSync(dirPath);
    files.forEach(function (file) {
      var filePath = dirPath + "/" + file;
      var stats = fs.statSync(filePath);
      if (stats.isDirectory()) {
        // console.log('/n讀取目錄:\n', filePath, "\n");
        readDirectory(filePath);
      } else if (stats.isFile() && /\.lua$/.test(filePath)) {
        var buff = fs.readFileSync(filePath);
        if (buff.length && buff[0].toString(16).toLowerCase() == "ef" && buff[1].toString(16).toLowerCase() == "bb" && buff[2].toString(16).toLowerCase() == "bf") {
          //EF BB BF 239 187 191
          console.log('\n發(fā)現(xiàn)BOM文件:', filePath, "\n");
          buff = buff.slice(3);
          fs.writeFile(filePath, buff.toString(), "utf8");
        }
        // { encoding: 'UTF-8', confidence: 0.99 }
        // var charset = chardet.detectFileSync(filePath);
        var info = jschardet.detect(buff);
        if (info.encoding == "GB2312" || info.encoding == "ascii") {
          var resultBuffer = encoding.convert(buff, "UTF-8", info.encoding);
          fs.writeFile(filePath, resultBuffer, "utf8");
        }
        else if (info.encoding != "UTF-8" && chardet.detectFileSync(filePath) != "UTF-8")
        {
          if (buff.toString().indexOf("\r\n") >?-1)
??????????{
????????????var?resultBuffer?=?encoding.convert(buff,?"UTF-8",?"GBK");
????????????fs.writeFile(filePath,?resultBuffer,?"utf8");
??????????}
????????}
??????}
????});
??}?else?{
????console.log('Not?Found?Path?:?',?dirPath);
??}
}
readDirectory(path);

注意上面的判斷,第一個(gè)明確是 GB2312或者ascii時(shí),直接將相應(yīng)的編碼轉(zhuǎn)為 utf-8。而如果返回是格式,先判斷是否有PC下的換行符,如果有則全部將它視為GBK進(jìn)行處理。

整個(gè)思路其實(shí)是比較簡(jiǎn)單,難點(diǎn)在于如果判斷文件編碼格式。這個(gè)真的很難>_<,獲取原編碼格式后,調(diào)用 encoding.convert(buff, 目標(biāo)編碼格式 , 原始編碼格式 ); 便可得到所需要的編碼。如果有空而且有興趣,可以下載Notepad++的源碼,看它是如何判斷文件的編碼格式

相信看了本文案例你已經(jīng)掌握了方法,更多精彩請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!

推薦閱讀:

js基礎(chǔ)提升學(xué)習(xí)之三種內(nèi)置對(duì)象

js基礎(chǔ)提升學(xué)習(xí)之基本數(shù)據(jù)類型

The above is the detailed content of Using Node.js to convert file encoding formats. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Knowledge graph: the ideal partner for large models Knowledge graph: the ideal partner for large models Jan 29, 2024 am 09:21 AM

Large language models (LLMs) have the ability to generate smooth and coherent text, bringing new prospects to areas such as artificial intelligence conversation and creative writing. However, LLM also has some key limitations. First, their knowledge is limited to patterns recognized from training data, lacking a true understanding of the world. Second, reasoning skills are limited and cannot make logical inferences or fuse facts from multiple data sources. When faced with more complex and open-ended questions, LLM's answers may become absurd or contradictory, known as "illusions." Therefore, although LLM is very useful in some aspects, it still has certain limitations when dealing with complex problems and real-world situations. In order to bridge these gaps, retrieval-augmented generation (RAG) systems have emerged in recent years. The core idea is

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

How to implement an online collaborative editor using WebSocket and JavaScript How to implement an online collaborative editor using WebSocket and JavaScript Dec 17, 2023 pm 01:37 PM

Real-time collaborative editors have become a standard feature of modern web development, especially in various team collaboration, online document editing and task management scenarios. Real-time communication technology based on WebSocket can improve communication efficiency and collaboration effects among team members. This article will introduce how to use WebSocket and JavaScript to build a simple online collaborative editor to help readers better understand the principles and usage of WebSocket. Understand the basic principles of WebSocketWebSo

See all articles