Atitit 按照行读取文件 目录 1.1. 类库'readline' 1 1.2. 类库C:\0workspace\dataindexGenerNodejs\sdk\io\FileUtils.js

Atitit 按照行读取文件

 

目录

1.1. 类库'readline' 1

1.2. 类库C:\0workspace\dataindexGenerNodejs\sdk\io\FileUtils.js 1

1.3. 使用 2

2. ref 3

 

 

    1. 类库'readline'

var readline = require('readline');

 

 

 

    1. 类库C:\0workspace\dataindexGenerNodejs\sdk\io\FileUtils.js

 

function readLines()

{

    var fs = require('fs');

    

 

}

 

function readLinesFromtxt(fReadName,callback)

{

    var fs = require('fs');

   

    var readline = require('readline');

    var fRead = fs.createReadStream(fReadName);

    var objReadline = readline.createInterface({

        input:fRead

    });

    var arr = new Array();

    objReadline.on('line',function (line) {

        arr.push(line);

        //console.log('line:'+ line);

    });

    objReadline.on('close',function () {

       // console.log(arr);

      callback(arr);

     //console.log("close evnt");

 

    });

 

}

 

exports.readLinesFromtxt =readLinesFromtxt

 

    1. 使用

 

 FileUtils = require("./sdk/io/FileUtils.js");

   FileUtils.readLinesFromtxt(newLocalfunction (list) {

      var newdir = 'c:\\000tmp\\' + path.filename + "Fld"

 

      try { fs.mkdirSync(newdir); } catch (e) { }

 

      console.log(list);

 

      for (line of list) {

         var pathM = require('path');

         var basename = pathM.basename(line)

         var lineFileName = newdir + "\\" + basename + ".txt"

         try {

            fs.writeFileSync(lineFileNameline);

         } catch (error) {

 

         }

 

         //  console.log(key);

      }

   });

 

})

 

 

  1. ref

 

var readline = require('readline');

 

/*

* 按行读取文件内容

* 返回:字符串数组

* 参数:fReadName:文件名路径

*      callback:回调函数

* */

function readFileToArr(fReadName,callback){

    var fRead = fs.createReadStream(fReadName);

    var objReadline = readline.createInterface({

        input:fRead

    });

    var arr = new Array();

    objReadline.on('line',function (line) {

        arr.push(line);

        //console.log('line:'+ line);

    });

    objReadline.on('close',function () {

       // console.log(arr);

        callback(arr);

    });

}

————————————————

版权声明:本文为CSDN博主「yajie_china」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/yajie_china/article/details/79407851

 

(9+条消息)【Node.js】'readline' 逐行读取、写入文件内容 - Sodino的专栏 - CSDN博客.mhtml

posted @ 2019-09-22 14:59  attilaxAti  阅读(5)  评论(0编辑  收藏  举报