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

    fs 文档 fs 文件系统 处理文件的模块 fs.readFile 读取文件 例子 fs.writeFile 写入...

  • fs的核心模块及方法

    fs的核心模块及方法 fs模块(http://nodejs.cn/api/)

  • nodejs-模块

    nodejs模块 一、nodejs模块分类 1.核心模块 Core Module、内置模块、原生模块 fs模块 p...

  • 6_Node 文件系统

    [toc] NodeJS 文件系统[fs 模块] 1.NodeJS 文件系统 NodeJS 文件系统被封装在fs模...

  • nodejs

    cannot find module fs/promises 在模块中,旧版nodejs是require("fs"...

  • node js(四)

    fs模块 fs概述 -在 NodeJS 中,所有与文件操作都是通过 fs 核心模块来实现的,包括文件目录的创建、删...

  • vue-cli脚手架基础实现

    commander chalk Inquirer.js nodejs fs、net模块

  • 9/12 课堂笔记

    nodejs的模块 nodejs文件操作 nodejs的io键盘交互 nodejsURL判断渲染模板 fs.wri...

  • nodejs——fs模块

    fs模块包含所有文件查询和处理的相关函数,用这些函数,可以查询文件信息,读写和关闭文件。 使用fs.stath函数...

  • NodeJs基础学习--fs模块(7)

    在nodeJs中,所有的文件操作都是通过fs核心模块来实现的,包括文件目录的创建、删除、查询以及文件的读取和写入,...

网友评论

    本文标题:nodejs 基础模块 fs

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