当前位置:网站首页>--fs module--
--fs module--
2022-08-02 03:39:00 【cjx177187】
====>读取文件
fs.readFile(path,(err,data)=>{}) 【异步函数,Whoever finishes reading first returns first】
- path 填路径 可以绝对可以相对
- err代表读取失败、data代表读取成功 它是一个buffer类型的数据(二进制数据流) 可以通过toString()Convert to String Note:
- res.end()方法接受 字符串或者buffer
fs.readFile(__dirname+"/01/01.html ",(err,data)=>{
console.log(data)
})
====>读取目录
fs.readdir(path,(err,data)=>{})
//读取目录
fs.readdir(__dirname,(err,arr)=>{
console.log(arr)
})
//Read the directory in the current file's folder
fs.readdir(__dirname+"/01/01.txt",(err,arr)=>{
console.log(arr)
})
//读取01文件夹中的01.txtA directory within the owning folder
====>写入文件(创建文件)
fs.writeFile(path ,data,callback(er))
如果文件存在,该方法写入的内容会覆盖旧的文件内容,不存在就创建一个新的path - 文件路径data - 要写入文件的数据,可以是 String(字符串) callback - 回调函数,回调函数只包含错误信息参数(err),在写入失败时返回.
fs.writeFile(__dirname+"/01/01.txt","hello",(err)=>{
console.log(err)
})
//写入文件:文件存在会覆盖,文件不存在会报错
//在01文件夹下新建一个01.txt文件,里面写入hello
====>删除文件
fs.unlink(path,callback(err))
path - 文件路径.callback(err) - 删除回调函数,err错误信息
//删除文件:If you encounter permission problems, you will not be able to delete it
fs.unlink(__dirname+"/01/01.txt",(err)=>{
console.log(err)
})
====>以追加方式写文件
fs.appendFile(__dirname + '/test.txt', '我会追加到文件内容的末尾', function (er) { console.log('追加内容完成');})
//拼接内容:The file will also be created without it
fs.appendFile(__dirname+"/01/01.txt","6666",(err)=>{
console.log(err)
})
//在01文件夹,01.txt文件中添加内容666
====>创建目录
fs.mkdir(__dirname + '/test', function (err) { if(err) throw err console.log('创建目录成功')})
//创建一个目录
fs.mkdir(__dirname+"/01/01.txt",(err)=>{})
fs.mkdir(__dirname+"/01/01.jpg",(err)={})//创建一个名叫01.jpg的文件夹,不是创建一个01的图片
====>移动/重命名文件或目录
fs.rename(oldPath, newPath, callback);
整个功能相当于重命名一个文件/文件夹路径
oldPath, 原目录/文件的完整路径及名;
newPath, 新目录/文件的完整路径及名;如果新路径与原路径相同,而只文件名不同,则是重命名callback(err), 操作完成回调函数;
err操作失败对象
移动文件有一个bug:The new path and the old path must be the same根盘
//移动文件
var oldpath=__dirname+"/01/01.txt"
var newpath=__dirname+"/01/02/01.txt"
fs.rename(oldPath, newPath, (err)=>{});
//将01文件夹下的01.txt移动到01文件夹下的02文件夹中
//修改文件名字
var oldpath=__dirname+"/01/01.txt"
var newpath=__dirname+"/01/02.txt"
fs.rename(oldPath, newPath, (err)=>{});
//将01文件夹下的01.txt改名为02.txt
====>拷贝文件
fs.copy(oldPath, newPath, callback);
oldPath, 原文件的完整路径;
newPath, 新文件的完整路径;
callback(err), 操作完成回调函数;
err操作失败对象
拷贝文件以后两个文件都会存在磁盘中===>【添加回调函数,利用unlinkDelete the files in the original folder】
//拷贝文件
var oldpath=__dirname+"/01/01.txt"
var newpath=__dirname+"/01/02/01.txt"
fs.copyFile(oldPath, newPath, (err)=>{});
//将01文件夹中的01.txt拷贝到01文件夹下的02文件夹中
边栏推荐
- docker中配置mysql 5.7
- 排序学习笔记(二)堆排序
- Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
- 【手把手带你学nRF52832/nRF52840 · (1)开发环境搭建】
- 小程序 van-cell 换行能左对齐问题
- STM32 触发HardFault_Handler如何查找原因
- URL module
- A senior test engineer asked me these questions as soon as the interview came
- pyppeteer使用样例脚本
- The @autowired distinguished from @ the Resource
猜你喜欢

parser = argparse.ArgumentParser()解析

【 application 】 life many years of operations, what turned scored 12 k + annual bonus salary?

FreeRTOS内核详解(1) —— 临界段保护原理

Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案

Chemical reagent Phospholipid-polyethylene glycol-hydroxyl, DSPE-PEG-OH, DSPE-PEG-Hydroxyl, MW: 5000

mysql阶段总结

js基础知识

AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘

Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000

ThunderBirde无法登录问题、pycharm调试一直收集数据、RuntimeError: CUDA error: device-side assert triggered等疑难杂症解决
随机推荐
js 数组去重的常用方法
js作用域与闭包
The querystring module
最新,每天填坑,Jeston TX1 精卫填坑,第一步:刷机
区间问题 : 今年暑假不AC
C语言中关于2的n次方求值问题(移位运算)
URL module
[Basic Tutorial of Remote Control Development 1] Crazy Shell Open Source Formation Drone-GPIO (Remote Control Indicator Light Control)
这些JS题面试时一定要答对!
由中序遍历和后序遍历得到前序遍历(树的遍历)
Scientific research reagent DMPE-PEG-Mal dimyristoylphosphatidylethanolamine-polyethylene glycol-maleimide
STM32 map文件解析
Guangzhou Huawei Interview Summary
页面加载流程
DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine supply, MW: 5000
Redis简单学习笔记
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
Customer Rating Control
DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified active group