nodejs 基础模块 fs

作者: 辉夜乀 | 来源:发表于2017-12-07 12:01 被阅读53次

    fs 文档

    fs 文件系统

    处理文件的模块

    fs.readFile

    读取文件

    image
    image

    例子


    image
    const fs = require('fs');
    const chalk = require('chalk');
    
    const result = fs.readFile('./readfile.js', 'utf8', (err, data) => {
      if (err) {
        console.log(err);
      } else {
        console.log(chalk.yellow(data));
      }
    });
    
    image

    fs.writeFile

    写入文件

    image
    image

    例子


    image
    const fs = require('fs');
    
    let message = 'hello world';
    
    fs.writeFile('./test.txt', message, {
      encoding: 'utf8'
    }, () => {
      console.log('done!');
    });
    
    image

    fs.stat

    查看文件信息

    image

    例子:

    image
    const fs = require('fs');
    
    fs.stat('./stat.js', (err, stats) => {
      if (err) throw err;
    
      console.log(stats.isFile());
      console.log(stats.isDirectory());
      console.log(stats);
    });
    
    image

    fs.rename

    文件重命名

    image

    例子

    image
    const fs = require('fs');
    
    fs.rename('./test.txt', './hello.js', err => {
      if (err) throw err;
      console.log('rename done');
    });
    

    fs.unlink

    删除文件

    image

    例子

    image
    const fs = require('fs');
    
    fs.unlink('./hello.js', err => {
      if (err) throw err;
      console.log('unlink done');
    });
    

    fs.readdir

    读取文件夹

    image

    例子

    image
    const fs = require('fs');
    
    fs.readdir('./', (err, files) => {
      if (err) throw err;
      console.log(files);
    });
    
    
    image

    fs.mkdir

    创建文件夹

    image

    例子

    image
    const fs = require('fs');
    
    fs.mkdir('./world', err => {
      if (err) throw err;
      console.log('mkdir done');
    });
    

    fs.rmdir

    删除文件夹

    image

    例子

    image
    const fs = require('fs');
    
    fs.rmdir('./world', err => {
      if (err) throw err;
      console.log('rmdir done');
    });
    

    fs.watch

    监视文件的变化,返回的对象是一个 fs.FSWatcher

    image

    例子

    image
    const fs = require('fs');
    
    fs.watch('./fs-watch.js', {
      recursive: true, /*是否递归,监视文件内的子文件*/
    }, (eventType, filename) => {
      console.log(eventType, filename);
    });
    
    
    image

    fs.createReadStream

    返回一个新建的 ReadStream 对象(详见可读流)。

    image
    image

    例子

    image
    const fs = require('fs');
    
    const rs = fs.createReadStream('./fs-createreadstream.js');
    
    rs.pipe(process.stdout);  /*stdout 就是控制台*/
    
    image

    fs.createWriteStream

    返回一个新建的 WriteStream 对象(详见可写流)。

    image
    image

    例子

    image
    const fs = require('fs');
    
    const ws = fs.createWriteStream('./hello.txt');
    
    const tid = setInterval(() => {
      const num = Math.floor(Math.random() * 10);
      console.log(num);
      if (num < 9) {
        ws.write(num + '');   /* write 的必须是 Buffer 或 string 类型参数 */
      } else {
        clearInterval(tid);
        ws.end();
      }
    }, 200);
    
    ws.on('finish', () => {
      console.log('finish done');
    });
    
    image
    image

    promisify

    解决异步回调地狱的方法

    例子:这里写了 2 种方法,promise 和 async await

    image
    const fs = require('fs');
    
    const {promisify} = require('util');
    
    const read = promisify(fs.readFile);
    
    
    // read('./promisify.js').then(data => {
    //   console.log(data.toString());
    // }).catch(err => {
    //   console.log(err);
    // });
    
    async function test() {
      try {
        const content = await read('./promisify.js');
        console.log(content.toString());
      } catch (err) {
        console.log(err);
      }
    }
    
    test();
    
    image

    相关文章

      网友评论

        本文标题:nodejs 基础模块 fs

        本文链接:https://www.haomeiwen.com/subject/ivddixtx.html