/** * Created by jjcheng on 2017/7/18. */ /*//輸出到終端 process.stdout.write("Hello World!" + "\n"); //經過參數讀取 process.argv.forEach(function(val, index, array){ console.log(index + ": " + val); }); //獲取執行路徑 console.log(process.execPath); //平臺信息 console.log(process.platform); //輸出當前目錄 console.log("當前目錄:" + process.cwd()); //輸出當前版本 console.log("當前版本:" + process.version); //輸出內存使用狀況 console.log(process.memoryUsage());*/ /* var util = require("util"); function Base(){ this.name = "base"; this.base = 1991; this.sayHello = function(){ console.log("Hello " + this.name); } } Base.prototype.showName = function(){ console.log(this.name); }; function Sub(){ this.name = "sub"; } util.inherits(Sub, Base); var objBase = new Base(); objBase.showName(); objBase.sayHello(); console.log(objBase); var objSub = new Sub(); objSub.showName(); //objSub.sayHello(); console.log(objSub); */ /* var util = require("util"); function Person(){ this.name = "byvoid"; this.toString = function(){ return this.name; }; } var obj = new Person(); console.log(util.inspect(obj)); console.log(util.inspect(obj, true)); console.log("util.isArray([])="+util.isArray([])); console.log(util.isArray(new Array)); console.log(util.isArray({})); console.log(util.isRegExp(/some regexp/)); console.log(util.isRegExp(new RegExp("another regexp"))); console.log(util.isRegExp({})); console.log(util.isDate(new Date())); console.log(util.isDate(Date()));//false (without "new" returns a Sring console.log(util.isDate({})); console.log(util.isError(new Error())); console.log(util.isError(new TypeError())); console.log(util.isError({name:"Error", message:"an error occurred"})); */ /* var filePath = "D:\\WebstormProjects\\node1\\input.txt"; var fs = require("fs"); //異步讀取 fs.readFile(filePath, function(err, data){ if(err){ return console.error(err); } console.log("異步讀取:" + data.toString()); }); //同步讀取 var data = fs.readFileSync(filePath); console.log("同步讀取:" + data.toString()); console.log("程序執行完畢。"); */ /* var filePath = "D:\\WebstormProjects\\node1\\input.txt"; var fs = require("fs"); //異步打開文件 console.log("準備打開文件!"); fs.open(filePath, "r+", function(err, fd){ if(err){ return console.error(err); } console.log("文件打開成功!"); }); */ /* var filePath = "D:\\WebstormProjects\\node1\\input.txt"; var fs = require("fs"); fs.stat(filePath, function(err, stats){ console.log(stats.isFile()); }); */ /* var filePath = "D:\\WebstormProjects\\node1\\input.txt"; var fs = require("fs"); console.log("準備打開文件!"); fs.stat(filePath, function(err, stats){ if(err){ return console.error(err); } console.log(stats); console.log("讀取文件信息成功!"); //檢測文件類型 console.log("是否爲文件(isFile)?" + stats.isFile()); console.log("是否爲目錄(isDirectory)?" + stats.isDirectory()); }); */ /* var filePath = "D:\\WebstormProjects\\node1\\input.txt"; var fs = require("fs"); console.log("準備寫入文件"); fs.writeFile(filePath, "我是經過寫入的文件內容!", function(err){ if(err){ return console.error(err); } console.log("數據寫入成功!"); console.log("------我是分割線------"); console.log("讀取寫入的數據"); fs.readFile(filePath, function(err, data){ if(err){ return console.error(err); } console.log("異步讀取文件數據:" + data.toString()); }); }); */ /* var filePath = "D:\\WebstormProjects\\node1\\input.txt"; var fs = require("fs"); var buf = new Buffer(1024); console.log("準備打開已存在的文件!"); fs.open(filePath, "r+", function(err, fd){ if(err){ return console.error(err); } console.log("文件打開成功!"); console.log("準備讀取文件愛你:"); fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){ if(err){ console.log(err); } console.log(bytes + " 字節被讀取"); //僅輸出讀取的字節 if(bytes > 0){ console.log(buf.slice(0, bytes).toString()); } }); });*/ /* var filePath = "D:\\WebstormProjects\\node1\\input.txt"; var fs = require("fs"); var buf = new Buffer(1024); console.log("準備打開文件!"); fs.open(filePath, "r+", function(err, fd){ if(err){ return console.error(err); } console.log("文件打開成功!"); console.log("準備讀取文件!"); fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){ if(err){ console.log(err); } //僅輸出讀取的字節 if(bytes > 0){ console.log(buf.slice(0, bytes).toString()); } //關閉文件 fs.close(fd, function(err){ if(err){ console.log(err); } console.log("文件關閉成功"); }); }); }); */ /* var filePath = "D:\\WebstormProjects\\node1\\input.txt"; var fs = require("fs"); var buf = new Buffer(1024); console.log("準備打開文件"); fs.open(filePath, "r+", function(err, fd){ if(err){ return console.error(err); } console.log("文件打開成功!"); console.log("截取10字節後的文件內容。"); //截取文件 fs.ftruncate(fd, 10, function(err){ if(err){ console.log(err); } console.log("文件截取成功。"); console.log("讀取相同的文件"); fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){ if(err){ console.log(err); } //僅輸出讀取的字節 if(bytes > 0){ console.log(buf.slice(0, bytes).toString()); } //關閉文件 fs.close(fd, function(err){ if(err){ console.log(err); } console.log("文件關閉成功!"); }); }); }); }); */ /* var filePath = "D:\\WebstormProjects\\node1\\input.txt"; var fs = require("fs"); console.log("準備刪除文件"); fs.unlink(filePath, function(err){ if(err){ return console.error(err); } console.log("文件刪除成功!"); }); */ /* var filePath = "D:\\WebstormProjects\\node1\\input.txt"; var fs = require("fs"); console.log("建立目錄"); fs.mkdir(filePath, function(err){ if(err){ return console.error(err); } console.log("目錄建立成功。"); });*/ /* var filePath = "D:\\WebstormProjects\\node1\\input.txt"; var fs = require("fs"); console.log("查看目錄"); fs.readdir(filePath, function(err, files){ if(err){ return console.error(err); } files.forEach(function(file){ console.log(file); }); }); */ /* var filePath = "D:\\WebstormProjects\\node1\\input.txt"; var fs = require("fs"); console.log("準備刪除目錄"); fs.rmdir(filePath, function(err, files){ if(err){ return console.error(err); } console.log("讀取目錄"); fs.readdir(filePath, function(err, files){ if(err){ return console.error(err); } files.forEach(function(file){ console.log(file); }); }); }); */