lineReader 文件上传MySQL(可读每行)

来源:互联网 发布:出身不好知乎 编辑:程序博客网 时间:2024/06/05 14:52
var fs = require("fs");
var lineReader = require('line-reader');
var async = require("async");
var readFile= require("./路径/readFileDao");


function run(){
var path = 'E:/';    //设置读取文件路径
explorer(path);
}
run();

function explorer(path){
fs.readdir(path, function(err,files){    //读取路径下所有文件
if(err){
console.log("error:\n"+err);
return;
}else{
async.mapSeries(files, function(file, callback) {    //将文件逐一循环出
var index = 0;
var id = "";
var title = "";
var content = "";

lineReader.eachLine(path+file, function(line, last) {  //path+file请保证为文件路径+文件名  line为文件每行内容
index ++;
if (index == 1) {
id = id + line;
} else if (index == 2) {
title = title + line;
} else if (index >= 4) {
content = content + line;
}
if(last){       //判断如为最后一行执行
var orderThing = {};
orderThing.id = id;
orderThing.title = title;
orderThing.content = content;

        readFile.readFile中的自定接口(orderThing, function(err, result){
     if(err){
          callback(err);
     return;
  }else{
     callback(null);
return;
  }
    });
}
});


}, function(err) {    
if(err){
console.info(err);
console.info('read data error');
return;
}else{
console.info('read data success');
return;
}
});

}
});
}
0 0