如何找出给定文本文件的编码字符?
var inputFile = "filename.txt";
var file = fs.readFileSync(inputFile);
var data = new Buffer(file, "ascii");
var fileEncoding = some_clever_function(file);
if (fileEncoding !== "utf8") {
// do something
}
由于
答案 0 :(得分:3)
您可以尝试使用外部模块,例如https://www.npmjs.com/package/detect-character-encoding
答案 1 :(得分:1)
前面提到的模块也适用于我。或者,您可以查看我目前正在使用的 detect-file-encoding-and-language。
安装:
$ npm install detect-file-encoding-and-language
用法:
// index.js
const languageEncoding = require("detect-file-encoding-and-language");
const pathToFile = "/home/username/documents/my-text-file.txt"
languageEncoding(pathToFile).then(fileInfo => console.log(fileInfo));
// Possible result: { language: japanese, encoding: Shift-JIS, confidence: { language: 0.97, encoding: 1 } }