/**
* @description fs模块常用api
*/ // fs所有的文件操作都是异步IO,如果要以同步的方式去调用,都会加一个在原同步api的基础上加Sync
// 同步的方式会在最后传入一个callback,异步的方式不会传callback
// 异步的缺陷就是无法控制异常,只能用try catch const fs = require('fs');
const path = require('path'); const fileReadPath = path.resolve(__dirname, 'demoRead.txt'); // 读取文件
// readFile readFileSync
try {
const data = fs.readFileSync(fileReadPath, 'utf8');
// console.log(data)
} catch(err) {
console.error('读取文件出错: ', err.message)
fs.writeFileSync(fileReadPath, '123,123,423');
} fs.readFile(fileReadPath, 'utf8', function(err, data) {
if (err) return console.error('读取文件出错: ', err.message)
console.log('文件内容: ', data)
}) // 读取文件夹,相当于获取当前文件夹内所有目录
// readdir readdirSync
// 返回一个数组,里面是所有文件的路径path
const readdir = fs.readdirSync(path.resolve(__dirname, '../fs'));
console.log(readdir) const fileWritePath = path.resolve(__dirname, 'demoWrite.html'); // 写入文件
// 异步的写入,callback传的是error
// writeFile writeFileSync
fs.writeFile(fileWritePath, '<html>123123</html>', function(err) {
if (err) throw err;
// console.log('写入成功')
}) try {
fs.writeFileSync(fileWritePath, '<html>123123</html>');
// console.log('写入成功')
} catch(err) {
console.log(err)
} const dirPath1 = path.resolve(__dirname, 'demoDir1'); // 创建目录
// mkdir mkdirSync
fs.mkdir(dirPath1, function(err) {
if (err) {
// console.log(err)
return
};
// console.log('创建目录成功')
}) const dirPath2 = path.resolve(__dirname, 'demoDir2'); try {
fs.mkdirSync(dirPath2);
fs.writeFileSync(dirPath2 + '/demoDir2File.txt', 'dqwdjwdojdojqwd');
fs.mkdirSync(dirPath2 + '/balabala');
fs.writeFileSync(dirPath2 + '/balabala/balabalaFile.txt', 'dqwdjwdojdojqwd');
} catch(err) {
// console.log(err);
} // 检查是否存在
// existsSync
const isExists = fs.existsSync(path.resolve(__dirname, 'demoRead.txt'));
console.log('isExists: ', isExists) // callback的exists已经快不用了,现在用access
fs.access(path.resolve(__dirname, 'demoRead.txt'), function(err) {
if (err) console.log(err)
console.log('有这个文件的')
}); // 判断是一个文件夹还是一个文件,先获取文件stat,然后调用方法
// stat statSync
// isDirectory() isFile()
const stat = fs.statSync(path.resolve(__dirname, 'demoDir1'));
if (stat.isDirectory()) {
console.log('当前是一个文件夹')
} else if (stat.isFile()) {
console.log('当前是一个文件')
} else {
console.log('既不是文件也不是文件夹');
} // 删除文件
// unlink unlinkSync
const filePath = path.resolve(__dirname, 'demoRead.txt');
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
console.log('删除文件成功')
} // 删除文件夹
// rmdir rmdirSync
// 注意,如果文件夹里还有文件夹,则删除会报错,要删除的文件夹必须是全是文件,或者没有,不能有文件夹
// 如果有,必须递归删除
if (fs.existsSync(dirPath2)) {
try {
fs.rmdirSync(dirPath2);
} catch(err) {
delDir(dirPath2)
}
console.log('删除文件夹成功')
} // 递归删除文件或文件夹
function delDir(filePath) {
if (fs.existsSync(filePath)) {
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
// 当前是文件夹,需要判断里面是否还有文件夹
// 需要遍历文件夹内的所有文件,并判断是否是文件夹或文件,即递归调用
const files = fs.readdirSync(filePath);
files.forEach(function(item) {
delDir(path.resolve(filePath, item));
});
fs.rmdirSync(filePath);
} else if (stat.isFile()) {
// 当前是文件,直接删除
fs.unlinkSync(filePath);
}
} else {
console.log('不存在该文件!')
}
}

  

Nodejs模块:fs的更多相关文章

  1. nodejs模块——fs模块

    fs模块用于对系统文件及目录进行读写操作. 一.同步和异步 使用require('fs')载入fs模块,模块中所有方法都有同步和异步两种形式. 异步方法中回调函数的第一个参数总是留给异常参数(exce ...

  2. nodejs模块——fs模块 读取文件

    readFile读取文件 fs.readFile(filename,[option],callback) 方法读取文件. 参数说明: filename String 文件名 option Object ...

  3. 深入Nodejs模块fs - 文件系统操作

    node 的fs文档密密麻麻的 api 非常多,毕竟全面支持对文件系统的操作.文档组织的很好,操作基本分为文件操作.目录操作.文件信息.流这个大方面,编程方式也支持同步.异步和 Promise. 本文 ...

  4. nodejs模块fs——文件操作api

    // fs模块常用api // 读取文件 .写入文件 .追加文件. 拷贝文件 .删除文件 // 读取文件 // fs.readFile(path[, options], callback) // fs ...

  5. nodejs模块——fs模块 使用fs.write读文件

    fs.write() fs.read(fd,buffer,offset,length[,position],callback(err,bytesWritten,buffer))接收6个参数. 参数说明 ...

  6. nodejs模块——fs模块 使用fs.read读文件

    使用fs.read读文件 fs.read() 先介绍fs.open. fs.open(path,flags,[mode],callback)方法用于打开文件,以便fs.read()读取. 参数说明: ...

  7. nodejs模块——fs模块 WriteFile写入文件

    WriteFile写入文件 使用fs.writeFile(filename,data,[options],callback)写入内容到文件. 参数说明: filename String 文件名 dat ...

  8. Nodejs:fs模块 & rimraf模块

      模块fs:可以通过他管理文件系统,文件的写入,删除等操作 模块rimraf: 递归删除文件的node插件,在项目的文件编译之前,可以清除dist文件夹里的内容   API样例: var fs = ...

  9. nodejs 模块恩仇录

    anywhere 一句话:随时随地将你的当前目录变成一个静态文件服务器的根目录. 安装 npm install anywhere -g anywhere -h localhost -p 8060 fa ...

  10. nodejs nodejs模块使用及简单的示例

    nodejs模块使用及简单的示例 参考菜鸟教程网:http://www.runoob.com/ 一.fs模块的使用: 1.文件操作: 读文件: //读文件 var fs=require('fs'); ...

随机推荐

  1. centos7与centos6命令差异

    技术群: 816227112 查看ip centos6 : ifconfigcentos7 : ip addr 修改hostname centos6 : 修改/etc/sysconfig/networ ...

  2. Docker之Ubuntu上使用Docker的简易教程

    Ubuntu上使用Docker的简易教程 原始文档:https://www.yuque.com/lart/linux/fp6cla 说在开头 在天池的比赛中涉及到了docker的使用.经过多番探究,大 ...

  3. python 版本 jaeger-client 导入失败 jaeger-client-python

    环境为: OS: ubuntu18.04 Python: 3.6 问题原因: 尝试使用 jaeger-client-python,官方给出的示例(https://github.com/jaegertr ...

  4. Linux中文解决

    中文编码问题 安装中文语言包 locale -a | grep zh 查看是否有中文语言包 local 查看是否 LC_TYPE 为空 在 /etc/profile.d/ 下创建 lc_type.sh ...

  5. Vue中diff算法的理解

    Vue中diff算法的理解 diff算法用来计算出Virtual DOM中改变的部分,然后针对该部分进行DOM操作,而不用重新渲染整个页面,渲染整个DOM结构的过程中开销是很大的,需要浏览器对DOM结 ...

  6. Linux快速搭建C/C++开发环境

    导读:越来越多的程序员在Linux下进行C/C++的开发.本文以CentOS 7为例,教你快速搭建一个vi + gcc/g++ + Make + valgrind的开发环境. 本文字数:1500,阅读 ...

  7. java this关键字调用构造方法

    一 this调用构造方法 构造方法之间的调用,可以通过this关键字来完成. 格式: this(参数列表); 构造方法的调用举例: class Person { // Person的成员属性 priv ...

  8. 2020-07-23:开启rdb后,redis的启动流程是怎样的?

    福哥答案2020-07-23: Redis 在完成初始化全局服务器配置,加载配置文件,初始化服务器,开始加载持久化的数据到内存中.如果启用了 appendonly 了,则Redis从 appendfi ...

  9. 2020-06-07:mysql中varchar类型的id,where id=1,会用到索引吗?int 类型的id,where id="1",会用到索引吗?为什么?

    福哥答案2020-06-07: 答案来自群员:对于int类型id,查询的varchar 类型 ‘1’会隐式转换成 1,‘1’和 1都能正常走索引:对于varchar类型id,查询的int 类型 1不会 ...

  10. 2020-05-24:ZK分布式锁有几种实现方式?各自的优缺点是什么?

    福哥答案2020-05-24: Zk分布式锁有两种实现方式一种比较简单,应对并发量不是很大的情况.获得锁:创建一个临时节点,比如/lock,如果成功获得锁,如果失败没获得锁,返回false释放锁:删除 ...