node读取gb2312文件
作者:互联网
- 读取gbk gb2312 需要先安装iconv-lite : npm install iconv-lite
- gbk 格式问价,utf8 格式打开乱码
//1. 导入fs模块
const fs = require("fs")
const filepath = './testfile/yibao/log/呼叫2022-06-23.log'
var iconv = require("iconv-lite");
//return console.log(iconv);
let stat = fs.statSync(filepath);
console.log(stat.mtime);
// 2.调用fs.readFile()文件读取方法
fs.readFile(filepath,function(err,dataStr){
if(err){
return console.log(err);
}
var fileCon = iconv.decode(dataStr, 'GB2312').toString();
console.log(fileCon)
// console.log("---下面是文件内容---")
// console.log(dataStr)
})
标签:node,iconv,fs,console,log,filepath,gb2312,lite,读取 来源: https://www.cnblogs.com/pansidong/p/16408153.html