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;
  • 相关阅读:
    file类型允许的文件格式设置问题,“选择文件”打开缓慢
    利用验证码登录豆瓣页面
    python利用scrapy框架爬取起点
    python爬取大众点评并写入mongodb数据库和redis数据库
    python爬虫——建立IP池,将可用IP存放到redis
    python爬虫爬取大众点评并导入redis
    用python爬整本小说写入txt文件
    简单的爬百度一个搜索页面
    Python爬虫——爬豆瓣登录页面
    mysql数据库出现2003-Can't connect to MySQL server on 'localhost' (10061)的解决方法
  • 原文地址:https://www.cnblogs.com/miid/p/5289052.html
Copyright © 2011-2022 走看看