1、读取文件内容
var fs = require('fs');
// fs.readFile('./a.txt',function(err,data){utf8输出文本
fs.readFile('./a.txt','utf8',function(err,data){
if(err){
console.log(err);
return;
}
console.log(data);
})
2、按行读取文件中的中文,需正则匹配
var fs = require('fs');
var readline = require('readline');
let reg = /[\u4e00-\u9fa5]/g;
function readFileToArr(fReadName,callback){
var fRead = fs.createReadStream(fReadName);
var objReadline = readline.createInterface({
input:fRead
});
var arr = new Array();
objReadline.on('line',function (line) {
if(line.match(reg)){
arr.push(line.match(reg));
}
// console.log('line:'+ line);
});
objReadline.on('close',function () {
// console.log('close',arr);
callback(arr);
});
}
readFileToArr('./canvas.html',function(data){
data.forEach((item,index)=>{
console.log(index,item.join(''));
})
// console.log(data);
});