zoukankan      html  css  js  c++  java
  • Node.js_文件系统 FS

    文件系统 FS——File System

    所谓的文件系统,就是对计算机中的文件进行增、删、查、改等操作

    是一个服务器的基础

    node 通过核心 FS 模块来操作文件系统

    • 简单写

    // 1. 导入 fs 模块

    • const fs = require('fs');

    // 2. 简单写入文件

    • fs.writeFile('./hello.txt', 
          'Hello File System!', 
          {
              encoding:'utf-8',
              mode: 0o666,    // 4 2 0 可读 可写
              flag: 'a'    // 追加写操作
          }, 
          err=>{
              // 3. 判断方法是否出错
              if(err){    // 如果出错 err 为一个对象
                  console.log(err);
              }else{
                  console.log('文件写入成功');
              };
          }
      );
    • 流式写(可以一点一点写入内容)

    // 1. 导入 fs 模块

    • const fs = require('fs');

    // 2. 创建可写流

    • const ws = fs.createWriteStream('./test.txt');
      
      // 绑定监听事件,监听 可写流 有没有开始工作
      ws.on('open', ()=>{
          console.log('开始写了!');
      });
      
      // 绑定监听事件,监听 可写流 有没有开始工作
      ws.on('close', ()=>{
          console.log('写完了!');
      });    
      
      // ws.close();    会立即关闭 写入流,即使 文件流 内容还没有完全写入文件
      // ws.end();    // 等待 写入流 内容全部写入文件,再关闭

    // 3. 往 可写流 写入内容

    • ws.write('丫丫啦个呸的... ...');
      ws.write('啥玩意儿?!');

    // 4. 关闭写入流

    • ws.end();
    • 简单读

    // 1. 导入 fs 模块

    • const fs = require('fs');

    // 2. 读文件

    • fs.readFile(
          './package.json',
          (err, bufData)=>{
              if(err){
                  console.log(err);
              }else{
                  console.log(bufData.toString());
              };
          }
      );
    • 流式读(可以一点一点读)

    // 1. 导入 fs 模块

    • const fs = require('fs');

    // 2. 创建可读流

    • const rs = fs.createReadStream('D:\Audiio\test.mp4');
      
      // 绑定监听事件
      rs.on('open', ()=>console.log('开始读!'));
      rs.on('close', ()=>console.log('开始读!'));
      
      // 绑定读取文件事件
      rs.on('data', bufData=>{
          console.log('开始读!');
      });
    • 文件复制
    • const {createReadStream, createWriteStream} = require('fs');
      
      const ws = createWriteStream('./testCopy.mp4');
      
      // 绑定监听事件,监听 可写流 有没有开始工作
      ws.once('open', ()=>{
          console.log('开始写了!');
      });
      
      // 绑定监听事件,监听 可写流 有没有开始工作
      ws.once('close', ()=>{
          console.log('写完了!');
      });
      
      const rs = createReadStream('D:\Audiio\test.mp4');
      
      // 绑定监听事件
      rs.once('open', ()=>console.log('开始读!'));
      rs.once('close', ()=>{
          console.log('读完了!');
          ws.end();    // 等待 写入流 内容全部写入文件,再关闭
      });
      
      // 绑定读取文件事件
      rs.on('data', bufData=>{
          console.log('读ing');
          ws.write(bufData);
      });
    • 管道 读写 数据流
    • const {createReadStream, createWriteStream} = require('fs');
      
      const ws = createWriteStream('./testCopy.mp4');
      
      const rs = createReadStream('D:\Audiio\test.mp4');
      
      rs.pipe(ws);

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    5

    --------小尾巴 ________一个人欣赏-最后一朵颜色的消逝-忠诚于我的是·一颗叫做野的心.决不受人奴役.怒火中生的那一刻·终将结束...
  • 相关阅读:
    2014年5月16日
    2014年4月8日
    Qt 小技巧之“To-Do 事项”
    koa中间件实现分析
    关于计算透视投影的四条边的方法,留作备忘
    关于向量
    关于ngui协同
    关于NGUI分辨率
    动态修改NGUI UI2DSprite
    动态设置viewport的宽高
  • 原文地址:https://www.cnblogs.com/tianxiaxuange/p/10137702.html
Copyright © 2011-2022 走看看