1.直接读取
//引入
var fs = require('fs');
var path = 'text.json';
fs.readFile(path, function (error, data) {
if (error) {
console.log('读取文件失败了'+error)
} else {
data = JSON.parse(data.toString());
}
})
2.按行读取
//引入
var fs = require('fs');
var readline = require('readline');
//调用方法
var path = 'text.json';
read_file(path,function (data) {
console.log(data);
});
//定义读取方法
function read_file(path,callback){
var fRead = fs.createReadStream(path);
var objReadline = readline.createInterface({
input:fRead
});
var arr = new Array();
objReadline.on('line',function (line) {
console.log(line)
arr.push(JSON.parse(line));
});
objReadline.on('close',function () {
callback(arr);
});
}
版权声明:本文为weixin_42171955原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。