nodejs 基础模块 fs

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 文件系统模块是一个封装了标准的 POSIX 文件 I/O 操作的集合。通过require('fs')使用这个模块。...
    保川阅读 799评论 0 0
  • https://nodejs.org/api/documentation.html 工具模块 Assert 测试 ...
    KeKeMars阅读 6,368评论 0 6
  • 一、核心模块和对象 核心模块的意义 常用内置模块path:处理文件路径fs:操作文件系统child_process...
    EndEvent阅读 4,434评论 0 1
  • //公共引用 varfs =require('fs'), path =require('path'); 1、读取文...
    才気莮孒阅读 838评论 0 1
  • 诸神为你见证众星为你加冕将在这加冕为王星光为冠自此荣耀与公义必将显现
    但丁如是说阅读 217评论 0 0