Nodejs模块:fs
/**
* @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的更多相关文章
- nodejs模块——fs模块
fs模块用于对系统文件及目录进行读写操作. 一.同步和异步 使用require('fs')载入fs模块,模块中所有方法都有同步和异步两种形式. 异步方法中回调函数的第一个参数总是留给异常参数(exce ...
- nodejs模块——fs模块 读取文件
readFile读取文件 fs.readFile(filename,[option],callback) 方法读取文件. 参数说明: filename String 文件名 option Object ...
- 深入Nodejs模块fs - 文件系统操作
node 的fs文档密密麻麻的 api 非常多,毕竟全面支持对文件系统的操作.文档组织的很好,操作基本分为文件操作.目录操作.文件信息.流这个大方面,编程方式也支持同步.异步和 Promise. 本文 ...
- nodejs模块fs——文件操作api
// fs模块常用api // 读取文件 .写入文件 .追加文件. 拷贝文件 .删除文件 // 读取文件 // fs.readFile(path[, options], callback) // fs ...
- nodejs模块——fs模块 使用fs.write读文件
fs.write() fs.read(fd,buffer,offset,length[,position],callback(err,bytesWritten,buffer))接收6个参数. 参数说明 ...
- nodejs模块——fs模块 使用fs.read读文件
使用fs.read读文件 fs.read() 先介绍fs.open. fs.open(path,flags,[mode],callback)方法用于打开文件,以便fs.read()读取. 参数说明: ...
- nodejs模块——fs模块 WriteFile写入文件
WriteFile写入文件 使用fs.writeFile(filename,data,[options],callback)写入内容到文件. 参数说明: filename String 文件名 dat ...
- Nodejs:fs模块 & rimraf模块
模块fs:可以通过他管理文件系统,文件的写入,删除等操作 模块rimraf: 递归删除文件的node插件,在项目的文件编译之前,可以清除dist文件夹里的内容 API样例: var fs = ...
- nodejs 模块恩仇录
anywhere 一句话:随时随地将你的当前目录变成一个静态文件服务器的根目录. 安装 npm install anywhere -g anywhere -h localhost -p 8060 fa ...
- nodejs nodejs模块使用及简单的示例
nodejs模块使用及简单的示例 参考菜鸟教程网:http://www.runoob.com/ 一.fs模块的使用: 1.文件操作: 读文件: //读文件 var fs=require('fs'); ...
随机推荐
- Shiro探索1. Realm
1. Realm 是什么?汉语意思:领域,范围:王国:这个比较抽象: 简单一点就是:Realm 用来对用户进行认证和角色授权的 再简单一点,一个用户怎么判断它有没有登陆?这个用户是什么角色有哪些权限? ...
- 初识分布式:MIT 6.284系列(一)
前言 本系列是源于「码农翻身」所属知识星球发起的读书活动,由大佬 @我的UDP不丢包 推荐而来,这次的读书活动有一些另类,我们抛弃了传统的书籍,开始攻略最高学府的研究生顶级课程 <6.824&g ...
- GPS位置显示在地图上
源码分析博客地址:https://blog.csdn.net/wuquan_1230/article/details/79614974 工具下载地址:http://download.csdn.net/ ...
- 数据结构进阶:ST表
简介 ST 表是用于解决 可重复贡献问题 的数据结构. 什么是可重复贡献问题? 可重复贡献问题 是指对于运算 \(\operatorname{opt}\) ,满足 \(x\operatorname ...
- XCTF-WEB-新手练习区(9-12)笔记
9:xff_referer X老师告诉小宁其实xff和referer是可以伪造的. 界面显示需要我们 添加X-Forwarded-For:123.123.123.123 添加Rerferer:http ...
- C#LeetCode刷题之#231-2的幂(Power of Two)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3858 访问. 给定一个整数,编写一个函数来判断它是否是 2 的幂 ...
- C#LeetCode刷题之#290-单词模式(Word Pattern)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3778 访问. 给定一种 pattern(模式) 和一个字符串 s ...
- Vue Slots
子组件vue <template> <div> <slot v-if="slots.header" name="header"&g ...
- 关于什么时候用,怎么用:ExecuteNonQuery(),还有其它返回值
ExecuteScalar方法返回的类型是object类型,这个方法返回sql语句执行后的第一行第一列的值,由于不知到sql语句到底是什么样的结构(有可能是int,有可能是char等等),所以Exec ...
- PYTHON替代MATLAB在线性代数学习中的应用(使用Python辅助MIT 18.06 Linear Algebra学习)
前言 MATLAB一向是理工科学生的必备神器,但随着中美贸易冲突的一再升级,禁售与禁用的阴云也持续笼罩在高等学院的头顶.也许我们都应当考虑更多的途径,来辅助我们的学习和研究工作. 虽然PYTHON和众 ...