Node.js 文件系统数组
/*Node.js 的文件系统 */ var fs = require("fs"); var path = require("path"); var root = "folder"; var file = root + "/" + "file.txt"; var fileData = "***最近想跳河编写Nodejs File System 服务器端程序***" //建立文件夹,若存在就不建立了 fs.mkdir(root, function (err) { console.log(err ? "该文件夹已经存在,无需建立" : "建立文件夹成功!"); }); //在文件夹中建立文件 fs.exists(file, function (exits) {//回调函数,用参数判断文件(夹)是否存在 if (exits) { console.log("文件已经存在!") } else { fs.writeFile(file, fileData, function (err) { console.log(err ? "建立文件失败" : "建立文件成功"); }) } }); //读取全部文件夹中的全部文件与文件夹 function walkDirs(root){ console.log("根路径:"+root);//输出根路径 fs.readdir(root,function(err,entries){ for (var idx in entries){//回调函数,返回数组 //路径合并path.join var fullPath = path.join(root,entries[idx]); (function(fullPath){//匿名函数 //判断该路径下:是文件or文件夹 fs.stat(fullPath,function(err,stats){ //为文件,输出 if (stats && stats.isFile()){ console.log(fullPath); }else{//文件夹,递归 if(stats && stats.isDirectory()){ walkDirs(fullPath); }} }) })(fullPath) } }) } walkDirs(root);