nodejs 基础模块 fs

fs 文档

fs 文件系统

处理文件的模块

fs.readFile

读取文件

nodejs 基础模块 fs_第1张图片
image

nodejs 基础模块 fs_第2张图片
image

例子


nodejs 基础模块 fs_第3张图片
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));
  }
});
nodejs 基础模块 fs_第4张图片
image

fs.writeFile

写入文件

nodejs 基础模块 fs_第5张图片
image

nodejs 基础模块 fs_第6张图片
image

例子


nodejs 基础模块 fs_第7张图片
image
const fs = require('fs');

let message = 'hello world';

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

fs.stat

查看文件信息

nodejs 基础模块 fs_第8张图片
image

例子:

nodejs 基础模块 fs_第9张图片
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);
});
nodejs 基础模块 fs_第10张图片
image

fs.rename

文件重命名

nodejs 基础模块 fs_第11张图片
image

例子

nodejs 基础模块 fs_第12张图片
image
const fs = require('fs');

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

fs.unlink

删除文件

nodejs 基础模块 fs_第13张图片
image

例子

nodejs 基础模块 fs_第14张图片
image
const fs = require('fs');

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

fs.readdir

读取文件夹

nodejs 基础模块 fs_第15张图片
image

例子

nodejs 基础模块 fs_第16张图片
image
const fs = require('fs');

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

nodejs 基础模块 fs_第17张图片
image

fs.mkdir

创建文件夹

nodejs 基础模块 fs_第18张图片
image

例子

nodejs 基础模块 fs_第19张图片
image
const fs = require('fs');

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

fs.rmdir

删除文件夹

nodejs 基础模块 fs_第20张图片
image

例子

nodejs 基础模块 fs_第21张图片
image
const fs = require('fs');

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

fs.watch

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

nodejs 基础模块 fs_第22张图片
image

例子

nodejs 基础模块 fs_第23张图片
image
const fs = require('fs');

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

nodejs 基础模块 fs_第24张图片
image

fs.createReadStream

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

nodejs 基础模块 fs_第25张图片
image

nodejs 基础模块 fs_第26张图片
image

例子

nodejs 基础模块 fs_第27张图片
image
const fs = require('fs');

const rs = fs.createReadStream('./fs-createreadstream.js');

rs.pipe(process.stdout);  /*stdout 就是控制台*/
nodejs 基础模块 fs_第28张图片
image

fs.createWriteStream

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

nodejs 基础模块 fs_第29张图片
image

nodejs 基础模块 fs_第30张图片
image

例子

nodejs 基础模块 fs_第31张图片
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');
});
nodejs 基础模块 fs_第32张图片
image

image

promisify

解决异步回调地狱的方法

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

nodejs 基础模块 fs_第33张图片
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();
nodejs 基础模块 fs_第34张图片
image

你可能感兴趣的:(nodejs 基础模块 fs)