nodejs后端读取文本信息

文件代码

  

‘use strict‘;

const fs = require(‘fs‘);

var LineReader = function (path) {
    this._leftOver = ‘‘;
    this._EOF = false;
    this._filename;
    this._fd = 0;
    this._bufferSize = 1024;
    this._buffer = new Buffer(this._bufferSize);

    if (undefined !== path) {
        try {
            fs.statSync(path).isFile();
            this.open(path);
        }
        catch (exception) {
            console.log(path, ‘is not a file.‘);
            this._EOF = false;
            return;
        }
    }
}

LineReader.prototype.close = function () {
    var self = this;
    try {
        fs.closeSync(self._fd);
    }
    catch (exception) {
        console.log(‘closing file failed.‘);
    }
    self._EOF = true;
    self._fd = 0;
    return;
}

LineReader.prototype.next = function () {
    var self = this;

    if (0 == self._fd) {
        return;
    }

    var _idxStart = 0;
    var idx = 0;
    while ((self._leftOver.indexOf(‘\n‘, _idxStart)) == -1) {
        var read;
        try {
            read = fs.readSync(self._fd, self._buffer, 0, self._bufferSize, null)
        }
        catch (exception) {
            console.log(‘reading file failed.‘);
            self.close();
            return;
        }
        if (read !== 0) {
            self._leftOver += self._buffer.toString(‘utf8‘, 0, read);
        } else {
            try {
                fs.closeSync(self._fd);
            }
            catch (exception) {
                console.log(‘closing file failed.‘);
            }
            self._EOF = true;
            self._fd = 0;
            return;
        }
    }
    if ((idx = self._leftOver.indexOf(‘\n‘, _idxStart)) !== -1) {
        var line = self._leftOver.substring(_idxStart, idx);
        _idxStart = idx + 1;
        self._leftOver = self._leftOver.substring(_idxStart);
        _idxStart = 0;
        return line;
    }
}

LineReader.prototype.open = function (thePath) {
    var self = this;
    self._filename = thePath;

    if (0 !== self._fd) {
        self.close();
    }

    try {
        self._fd = fs.openSync(self._filename, ‘r‘);
    }
    catch (exception) {
        console.log(‘open(): ‘ + self._filename + ‘ not found.‘);
        self._EOF = true;
        return;
    }
    self._EOF = false;
    return;
}

module.exports = LineReader;

 

单文件读取

const LineByLine = require("../../lib/readlinesyn"); 
var fs =require("fs");
var filename = ‘sensitive.text‘;
var liner = new LineByLine();
let word = [];
liner.open( filename ); 
var theline;
while( !liner._EOF ){
  theline = liner.next();
  console.log( ‘READ LINE: ‘ + theline );
  if(theline != undefined){
    word.push(theline.replace("\r",""));
  }
}
liner.close();
 console.log("读取后:" + JSON.stringify(word))
 

 

文件夹读取

 

    const LineByLine = require("../../lib/readlinesyn"); 
    var fs =require("fs");
    let word = [];
    var dir = "./src/lib/mgck/"; 
    var liner = new LineByLine();  
    var files = fs.readdirSync( dir );
    
    for( index in files ) {
        var fileName = files[index];
        var r = fileName.search(".txt");
        if( -1 != r )
        {
            console.log( fileName );
            liner.open( dir + fileName );   
            var theline;  
            while( !liner._EOF )  {  
                theline = liner.next();  
                if(theline != undefined){
                   word.push(theline.replace("\r",""));
                }
            }  
            liner.close(); 
        }
    }
console.log("读取后:" + JSON.stringify(word))

 

nodejs后端读取文本信息

上一篇:如何查找元素对应事件的js代码,检测定位js事件


下一篇:cookie session ajax