文件系统 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

Node.js_文件系统 FS的更多相关文章

  1. Node.js 文件系统fs模块

    Node.js 文件系统封装在 fs 模块是中,它提供了文件的读取.写入.更名.删除.遍历目录.链接等POSIX 文件系统操作. 与其他模块不同的是,fs 模块中所有的操作都提供了异步的和 同步的两个 ...

  2. 33.Node.js 文件系统fs

    转自:http://www.runoob.com/nodejs/nodejs-module-system.html Node.js 提供一组类似 UNIX(POSIX)标准的文件操作API. Node ...

  3. Node.js 文件系统模块

    章节 Node.js 介绍 Node.js 入门 Node.js 模块 Node.js HTTP模块 Node.js 文件系统模块 Node.js URL模块 Node.js NPM Node.js ...

  4. node基础学习——操作文件系统fs

    操作文件系统fs 1.在Node.js中,使用fs模块来实现所有有关文件及目录的创建.写入及删除.在fs模块中,所有对文件及目录的操作都可以使用同步与异步两种方法,具有Sync后缀的方法均为同步方法. ...

  5. 运用node的文件系统模块批量修改文件名

      如果我们需要大批量修改一个文件中的名称,比如,删除文件名中的副本时,就可以借助node的文件系统模块,快捷快速的完成. 首先建立一个js文件(changeName.js),代码如下: // 引入f ...

  6. Node.js文件系统、路径的操作详解

    17173 17173 2 75 2014-12-12T05:06:00Z 2014-12-12T05:06:00Z 21 2735 15595 www.17173.com 129 36 18294 ...

  7. Node.JS文件系统解析

    1.Node.js 文件系统 var fs = require("fs") 2.异步和同步 读取文件内容的函数有异步的 fs.readFile() 和同步的 fs.readFile ...

  8. Node.js 文件系统

    Node.js 文件系统封装在 fs 模块是中,它提供了文件的读取.写入.更名.删除.遍历目录.链接等POSIX 文件系统操作. 与其他模块不同的是,fs 模块中所有的操作都提供了异步的和 同步的两个 ...

  9. Node.js文件系统、路径的操作函数

    Node.js文件系统.路径的操作函数 目录 Node.js文件系统.路径的操作函数 1.读取文件readFile函数 2.写文件 3.以追加方式写文件 4.打开文件 5.读文件,读取打开的文件内容到 ...

随机推荐

  1. Deepin linux 远程桌面无法被Ubuntu连接

    linux下远程桌面的工具还是有很多的,这个方法主要是解决Ubuntu自带的 Remmina无法远程 Deepin 桌面. 1.安装vncserver的基础服务,输入以下命令: sudo apt-ge ...

  2. InnoDB-MVCC与乐观锁

    最近通过<高性能MySQL>一书学习MySQL方面的知识,在看到书中所讲InnoDB-MVCC部分的时候,有一种强烈的感觉,这不就是乐观锁吗(入门级小学徒的疑惑脸)?当下便去网上以各种方式 ...

  3. CSS三种样式

    CSS 指层叠样式表 (Cascading Style Sheets): 1 内联样式:无法复用,在元素style内写 ,很少使用: 2 内部样式:在head元素内style属性内写,此样式可以被当前 ...

  4. Webform--LinQ 分页组合查询

    一.linq高级查 1.模糊查(字符串包含) public List<User> Select(string name) { return con.User.Where(r => r ...

  5. luogu P5286 [HNOI2019]鱼

    传送门 这题真的牛皮,还好考场没去刚( 这题口胡起来真的简单 首先枚举D点,然后对其他所有点按极角排序,同时记录到D的距离.然后按照极角序枚举A,那么鱼尾的两个点的极角范围就是A关于D对称的那个向量, ...

  6. 第29月第2天 charles

    1. https://www.jianshu.com/p/55a8c84e0f24

  7. win10安装VMware v14.1.1.28517

    一.下载 VMware v14.1.1.28517 下载地址(包含安装说明):http://www.downza.cn/soft/74728.html 二.VMware Workstation 14 ...

  8. L1-Day3

    L1-Day31.太阳从西边落下. [我的翻译]The sun falls in the west. [标准答案]The sun sets in the west. [对比分析]落下fall与set的 ...

  9. 2018-2019-1 20165234 实现mypwd

    实现mypwd(选做,加分) 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd 提交过程博客的链接

  10. Houdini SDF/Raymarching/等高曲面绘制

    1 , SDF <1> union  min(a,b) <2> intersect: max(a,b) <3> Substract  a-b  : if(b> ...