zoukankan      html  css  js  c++  java
  • 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('
    ', _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('
    ', _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("
    ",""));
      }
    }
    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("
    ",""));
                    }
                }  
                liner.close(); 
            }
        }
    console.log("读取后:" + JSON.stringify(word))
  • 相关阅读:
    Windows抓取密码总结
    PHP一句话简单免杀
    Shiro反序列化利用
    windows绕过杀软添加账户密码
    java基础-操作符
    微信小程序开发笔记(十一)--输入框计数
    微信小程序开发笔记(十)--多选标签效果
    微信小程序开发笔记(九)--单选标签效果
    微信小程序开发笔记(八)--路由跳转
    proxy_pass 代理转发
  • 原文地址:https://www.cnblogs.com/timipaul/p/13226284.html
Copyright © 2011-2022 走看看