zoukankan      html  css  js  c++  java
  • NodeJs文件处理模块

    //文件处理类
    var fs = require('fs');
    var path = require('path');
    var util = require('util');
    var File = {
        in : function(content,flag){
            this.writeToFile("app.log",content,flag);
        },
        on : function(content,flag){
            this.appendToFile("app.log",content,flag);
        },
        writeInFile : function(content,flag){
            this.writeToFile("app.log",content,flag);
        },
        writeToFile : function(sourceFile,content,flag){
            fs.writeFileSync(sourceFile,util.format(content));
            if(flag){
                console.log("内容写入成功");
            }
        },
        appendToFile : function(sourceFile,content,flag){
            fs.appendFileSync(sourceFile,"
    
    ***************    " +this.getTime()+"   **************
    
    ");
            fs.appendFileSync(sourceFile,util.format(content));
            if(flag){
                console.log("内容追加成功");
            }
        },
        //复制文件
        copyFile : function(sourceFile,targetFile,log){
            File.makeDirs(path.dirname(targetFile),function(){
                fs.writeFileSync(targetFile,fs.readFileSync(sourceFile));
                if(log){
                      console.log(sourceFile + '->' + targetFile + '复制成功');
                  }
            });
        },
        //文件复制到目录
        copyFileToDir :function(sourceFile,targetDir,targetName,log){
            //获取文件名
            var name = '';
            if(targetName != undefined){
                var sourceExt = path.extname(sourceFile);
                var targetExt = path.extname(targetName);
                //得到后缀一样的文件
                name = path.basename(targetName,targetExt) + sourceExt;
            }
            else{
                name = path.basename(sourceFile);
            }
            this.makeDirs(targetDir,function(){
                File.copyFile(sourceFile,path.join(targetDir,name),log);
            });
        },
        //目录复制
        copyDir : function(sourceDir,targetDir,log){
            var sourceDirFiles = this.getDirs(sourceDir);
            sourceDirFiles.forEach(function(item){
                //获取相对路径
                var relativePath = path.relative(sourceDir,item);
                var targetAbsolutePath = path.join(targetDir,relativePath);
                File.copyFile(item,targetAbsolutePath,log);
            });
            console.log("done");
        },
        makeDirs : function(filepath,callback){
            fs.exists(filepath,function(exists){
                if(exists){
                    callback();
                }
                else{
                    File.makeDirs(path.dirname(filepath),function(){
                        fs.mkdir(filepath,callback);
                    });
                }
            });
        },
        getDirs : function(dirpath,result){
            if(result == undefined){
                result = [];
            }
            var dirList = fs.readdirSync(dirpath);
            dirList.forEach(function(item){
                var cfile = path.join(dirpath,item);
                if(fs.statSync(cfile).isDirectory()){
                      File.getDirs(cfile,result);
                }else{
                      result.push(cfile);
                }
            });
            return result;
        },
        getTime:function (d){
            d = d || new Date();
            return d.getHours() + ":" + this.getTwo(d.getMinutes()) + ":" + this.getTwo(d.getSeconds());
        },
        getTwo:function(m){
            return m < 10 ? ("0" + m) : m;
        }
    }
     
     
    module.exports = File;
  • 相关阅读:
    JS中的if语句内如何加or使多个条件通过
    对计算属性中get和set的理解
    如何在vue里实现同步阻塞请求,请求完成之前不加载页面或组件?
    vue 路由传参 params 与 query两种方式的区别
    vue中通过路由跳转的三种方式
    vue生成单文件组件和组件嵌套步骤
    this.$router.push() 在新窗口怎么打开
    Vue路由获取路由参数
    vue的v-for循环普通数组、对象数组、对象、数字
    element el-cascader设置默认值
  • 原文地址:https://www.cnblogs.com/miid/p/5289052.html
Copyright © 2011-2022 走看看