## 核心模块
### File System
1. fs.stat(url,callback(err,stats))
~~~
const fs = require("fs");
fs.stat("../fs",function(err,stats){
if(err){
return console.log(err);
}
console.log(stats.isDirectory());
console.log(stats.isFile());
})
~~~
2.fs.readFile(url,callback(err,data))
~~~
const fs = require("fs");
fs.readFile("../2.txt", (err,data)=>{
if(err){
return console.log(err);
}
console.log(data.toString());
});
~~~
3.fs.readdir(url,callback(err,files));
~~~
const fs = require("fs");
fs.readdir(url,(err,files)=>{
console.log(files) //files is a array;
})
~~~
4.获取某个文件夹下所有的文件夹
~~~
const fs = require('fs');
fs.readdir("../../node系统模块", (err,files)=>{
// console.log(files);
let arr = [];
for(let i=0;i<files.length;i++){
let cur = files[i];
fs.stat('../../node系统模块/'+cur, (err,stats)=>{
if(err){
return console.log(err)
}
if(stats.isDirectory()){
arr.push(cur);
// console.log(arr)
}
if(i==files.length-1){
console.log(arr);
}
});
}
});
~~~
5.fs.writeFile(url,data,callback(err)}
~~~
//不存在就创建,存在就替换
fs.writeFile("2.txt","lorem is gold",(err){
if(err){
return console.log(err)}
console.log("the file has been saved");
})
~~~
6.fs.writeFile(url,data,callback(err))
~~~
//追加,不存在就创建,存在就追加
~~~