fs文件目录处理模块
fs.stat检测是文件还是目录
fs.stat('./html',(error,data)=>{
if(error){
console.log("没找到");
return
}
console.log("文件",data.isFile());
console.log("目录",data.isDirectory());
})
fs.mkdir 创建目录
fs.mkdir('./css',(err)=>{
err?console.log(err):console.log("创建成功");
})
fs.writeFile 创建写入文件
fs.writeFile('./html/index.html','hello!!\n',err=>{
err?console.log(err):console.log("写入成功");
})
fs.appendFile 追加文件
fs.appendFile('./html/index.html','hello!!',err=>{
err?console.log(err):console.log("追加成功");
})
fs.readFile 读取文件
fs.readFile('./html/index.html',(error,data)=>{
error?console.log(error):console.log(data.toString());
})
fs.readdir 读取目录
fs.readdir('./html',(error,data)=>{
error?console.log(error):console.log("读取目录",data);
})
fs.rename 重命名 | 移动文件
fs.rename('./html/index.html','./html/a.html',error=>{
error?console.log(error):console.log("重命名成功");
})
fs.rename("./html/a.html","./css/a.html",error=>{
error?console.log(error):console.log("移动成功");
})
fs.rmdir 删除目录
fs.rmdir('./a',error=>{
error?console.log(error):console.log("删除成功");
})
fs.unlink 删除文件
fs.unlink('./a',error=>{
error?console.log(error):console.log("删除成功");
})