nodeJS文件操作
const { log } = require("console"); const fs = require("fs"); const path = require("path"); const filename = path.resolve(__dirname, "./myfiles1.txt"); // console.log(filename) // fs.readFile(filename,(err,content)=>{ // console.log( 'read', content.toString('utf-8')) // }) // fs.writeFile(filename,'fsfsdfs',(err,content)=>{ // console.log('dsfsd',content) // }) // (async () => { // const result = await fs.promises.stat(filename); // console.log(result); // })(); class File { constructor(filename, name, ext, isFile, size, createTime, updateTime) { this.filename = filename; this.name = name; this.isFile = isFile; this.size = size; this.createTime = createTime; this.updateTime = updateTime; this.ext = ext; } async getContent(isBuffer = false) { if (this.isFile) { if (isBuffer) { return await fs.promises.readFile(this.filename); } else { return await fs.promises.readFile(this.filename, "utf-8"); } } return null; } async getChildren() { if (this.isFile) return []; let children = await fs.promises.readdir(this.filename); console.log(children); children = children.map((name) => { const result = path.resolve(this.filename, name); console.log(result); return File.getFile(result); }); return Promise.all(children); } static async getFile(filename) { const stat = await fs.promises.stat(filename); const name = path.basename(filename); const extName = path.extname(filename); const isFile = stat.isFile(); const size = stat.size; const createTime = new Date(stat.birthtime); const updateTime = new Date(stat.mtime); return new File( filename, name, extName, isFile, size, createTime, updateTime ); } } async function readdir(dirname){ const dirFile = await File.getFile(dirname) return dirFile.getChildren() } async function test() { // const filename = path.resolve(__dirname, "./myfiles/1.txt"); // const file = await File.getFile(filename); // console.log(await file.getContent()); const filename = await path.resolve(__dirname, "./myfiles"); console.log(await readdir(filename)) } test(); return const file = path.resolve(__dirname,'./myfiles/1.txt') //f返回readStream const rs = fs.createReadStream(file,{ encoding:null, highWaterMark:1 , //每次读取数量 如果encoding有值 那么每次读一个字符 如果没有值 那么每次读一个字节 autoClose:true //读完自动关闭 默认为true }) rs.on('open',()=>{ log('文件被打开了') }) rs.on('error',(e)=>{ log('出错了',e.message) }) rs.on('close',()=>{ log('文件关闭') }) // rs.close() //只有注册了这个事件 才会开始读 rs.on('data',(chunk)=>{ // log('读到了一部分',chunk) rs.pause() }) rs.on('end',()=>{ log('结束') }) rs.on('pause',()=>{ log('暂停') setTimeout(() => { rs.resume() }, 10); }) rs.on('resume',()=>{ log('恢复') }) // rs.pause() //暂停读取 出发pause事件 // rs.resume() //恢复读取 触发resume事件
const myfile = path.resolve(__dirname,'./myfiles/333.txt') const ws = fs.createWriteStream(myfile,{ flags:'a', //a append追加 w write覆盖 encoding:'utf-8', //默认utf-8 将写入的字符串 通过utf-8转为字节 highWaterMark:2 // 每次最多写入的字节数 跟encoding没有关系了 }) const flag = ws.write('a') //返回bool true表示写入通道没有填满 接下来数据可以直接写入 无须排队 log(flag) //false表示缓冲区满了 需要再内存排队 // for (let index = 0; index < 1024 * 1024 * 10 ; index++) { // ws.write('a') // } let i = 0 function write() { let flag = true; while (i<1024 *1024 *10 && flag) { flag = ws.write('a') i++ } } write() // 解决背压问题 防止内存积压 ws.on('drain',()=>{ //队列排满了再清空会触发drain排干事件 必须是排满后清空的 未排满清空不触发 log('可以再次写入了') write() }) ws.on('end',()=>{ // 未排满清空不触发 log('结束') }) // rs.pipe(ws) 读取流的管道跟写的串起来 数据读一块就写一块 避免写背压代码