node

fs

const fs = require('fs');

const data = fs.readFileSync('conf.js');//阻塞操作

console.log(data)

fs.readFile('./conf.js', (err, data) => {  //异步操作

    console.log(data)

});

//Promise

const { promisify } = require('util');

const readFile = promisify(fs.readFile);

readFile('./conf.js').then(data => console.log(data))

//实验警告

const { promises } = require('fs');

promises.readFile('./conf.js').then(data => console.log(data))




const fs = require('fs');

const rs= fs.createReadStream('./conf.js') // 读取流

const ws = fs.createWriteStream('./conf2.js') // 写入流

rs.pipe(ws);会重新创建一个js 复制



buffer 

const buf1 = Buffer.alloc(10);

console.log(buf1)

const buf2 = Buffer.from([1,2,3])

console.log(buf2)

//写入

buf1.write('hello')

console.log(buf1)

//读取

console.log(buf1.toString())

const buf4 = Buffer.concat([buf1,buf2])

console.log(buf4.toString())

你可能感兴趣的:(node)