node利用fs模組建立資料夾並寫入檔案
阿新 • • 發佈:2019-02-17
var fs = require("fs") ;
var txt = "Hello World";
function write(){
fs.exists(path, function (exists) { //path為資料夾路徑
var retTxt = exists ? retTxt = '檔案存在' : '檔案不存在';
if(retTxt == '檔案存在'){
fs.writeFile(path,txt,function (err) {
if (err){
return false;
} else{
console.log("寫入成功");
}
}) ;
}else if(retTxt == '檔案不存在'){
fs.mkdir(path, function (err) {
if(err){
return false;
}else{
fs.writeFile(path,txt,function (err) {
if (err){
return false;
} else{
console.log("寫入成功");
}
}) ;
}
}
}
}
}
write();