fs文件系统操作模块注:fs模块=>包括文件目录的创建、删除、查询、文件的读写等;在fs模块中,所有方法分为同步和异步两种实现,带sync后缀的方法为同步方法,不带sync后缀的方法为异步方法文件读取文件异步fs.readFile(path,transcodingrule,(err,data)=>{})synchronousletres=fs.readFileSync(path,Transcodingrules)异步写文件fs.writeFile(path,你要写的内容,(err)=>{})Overwrite同步写入fs.writeFileSync(path,你要写入的内容)Overwritewrite写入文件异步fs.appendFile(path,你要写入的内容,(err)=>{})Appendwrite同步fs.appendFileSync(path,whatyouwanttowrite)Appendwritedeleteasynchronousfs.unlink(path,(err)=>{})syncfs.unlinkSync(path)copyfileasyncfs.copyFile(originalpath,newpath,(err)=>{}){})syncfs.copyFileSync(原路径,新路径)重命名asyncfs.rename(原路径,新路径,(err)=>{})syncfs.renameSync(原路径,新路径)文件夹读取文件夹asyncfs.readdir(path,(err,data)=>{})syncletres=fs.readdirSync(path)创建文件夹asyncfs.mkdir(path,(err)=>{})syncfs.mkdirSync(path)deleteemptyfolderasyncfs.rmdir(path,(err))=>{})Synchronizefs.rmdirSync(path)判断文件或文件夹是否存在Synchronizefs.existsSync(path)存在返回true,不存在返回falseletfs=require("fs");让res=fs.existsSync("./dist/2.js");console.log(res)asynchronousfs.exist(path,(bool)=>{console.log(bool);})//bool返回布尔值获取文件或文件夹的信息asynchronousfs.stat(path,(err,data)=>{})同步letres=fs.statSync(path)流读写1、创建可读流2、创建可写流3、管道输出letfs=require("fs");让rs=fs.createReadStream("./src/1.mp4");让ws=fs.createWriteStream("./src/2.mp4");rs.pipe(ws)readabitandwriteadataeventasdata变化时触发rs.on("data",(thunk)=>{console.log(thunk);if(!ws.write(thunk)){rs.pause()//pause}})writeDrain完成后会被触发ws.on("drain",()=>{rs.resume();//resume})read结束事件rs.on("end",()=>{console.log("success")})
