node文件系统
- 引入模块
- fs.readFile(path[,option],callback)
fs.readFile('class.js','utf-8',(error,data)=>{
if(error)throw error;
eval(data);
console.log(data);
});
- fs.writeFile(url,data,callback)
let myStr=`你好啊朋友`
fs.writeFile('test.txt',myStr,(err)=>{
if(err)throw err;
})
- fs.readFileSync(url[,options])
var a=fs.readFileSync('test.txt','utf-8');
console.log(a);
- fs.writeFileSync(url[,options])
读入流以及写入流
- fs.createReadStream(url)
- fs.createWriteStream(url)
const gzip = zlib.createGzip();
const fs = require('fs');
const inp = fs.createReadStream('input.txt');
const out = fs.createWriteStream('input.txt.gz');
inp.pipe(gzip).pipe(out);