意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

如何用代码说明Node.Js文件处理模块

来源:恒创科技 编辑:恒创科技编辑部
2024-01-23 21:28:59
这篇文章主要介绍“如何用代码说明Node.Js文件处理模块”,有一些人在如何用代码说明Node.Js文件处理模块的问题上存在疑惑,接下来小编就给大家来介绍一下相关的内容,希望对大家解答有帮助,有这个方面学习需要的朋友就继续往下看吧。



//文件处理类
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,"\n\n***************    " +this.getTime()+"   **************\n\n");
        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;

以上就是关于“如何用代码说明Node.Js文件处理模块”的相关知识,感谢各位的阅读,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注恒创科技,小编每天都会为大家更新不同的知识。


如何用代码说明Node.Js文件处理模块

上一篇: Node.JS使用mongodb存储数据的操作包括什么 下一篇: 手机怎么远程登录云服务器?