node:你还在用readFile和writeFile?fs新方式快速读写文件,支持大文件读写,安全放心才是首选

  发布日期:   2018-11-01
  最新修改:   2020-10-27
  阅读次数:   359 次

文件操作

  • 优点:快速读写,支持大文件读写,支持自动关闭close

  • 啥也不说,先上代码吧,自己封装的:

    const fs = require('fs')
    const path = require('path')
    
    class FileUtils {
        constructor() {
    
        }
    
        checkFile(path) {
            return new Promise((resolve, reject) => {
                try {
                    let result = fs.existsSync(path);
                    if (result) {
                        resolve(true)
                    } else {
                        resolve(false)
                    }
                } catch (e) {
                    reject(e)
                }
            });
        }
    
        readFile(path, opts = 'utf8') {
            return new Promise((resolve, reject) => {
                try {
                    var input = fs.createReadStream(path, {
                        encoding: 'utf8',
                        autoClose: true,
                    });
    
                    var remaining = '';
    
                    input.on('data', function (data) {
                        remaining += data;
                        var index = remaining.indexOf('\n');
                        var last = 0;
                        while (index > -1) {
                            var line = remaining.substring(last, index);
                            last = index + 1;
                            func(line);
                            index = remaining.indexOf('\n', last);
                        }
    
                        remaining = remaining.substring(last);
                    });
    
                    input.on('end', function () {
                        resolve(remaining)
                    });
                } catch (e) {
                    console.error('Error writeObjectToJsonString:', e);
                    resolve(e)
                }
    
            })
        }
    
        writeFile(path, data, opts = 'utf8') {
            return new Promise((resolve, reject) => {
                var out = undefined;
                     out = fs.createWriteStream(path, {
                        encoding: 'utf8'
                    });
                    out.write(data,(error) => {
                        if (error) {
                            reject(error)
                        } else {
                            resolve()
                        }
                    });
                    out.end();
    
            })
        }
    
        mkdirs(dirname) {
            return new Promise((resolve, reject) => {
                if (!fs.existsSync(dirname)) {
                    fs.mkdir(dirname, {recursive: true}, (err) => {
                        if (err) {
                            reject(err)
                        } else {
                            resolve()
                        }
                    });
                } else {
                    resolve();
                }
    
            });
    
        }
    }
    
    module.exports = new FileUtils();

   转载规则

《node:你还在用readFile和writeFile?fs新方式快速读写文件,支持大文件读写,安全放心才是首选字》GajAngels 采用 知识共享署名-非商业性使用 4.0 国际许可协议 进行许可。