fs
操作磁盘中的文件,文件操作,io
操作
该笔记中记录的均为 异步操作
提示
javascript
// 同步操作
import fs from 'fs' // ES 导包
const fs = require('fs') // commonJS 导包
// 异步操作
import fs from 'fs/promises' // ES 导包
import fs = require('fs/promises') // commonJS 导包
1
2
3
4
5
6
7
2
3
4
5
6
7
readFile()
读取文件
appendFile()
创建新文件,或将数据添加到已有文件中
mkdir()
创建目录
rmdir()
只可删除空目录
rm()
删除文件或目录
rename()
重命名
copyFile()
复制文件
注意
在使用路径时,尽量使用安全计算路径,因为不同的运行形式(例如在 idea 和终端运行),路径可能会出错
readFile
读取文件
文件的结果是 Buffer
对象,Buffer
对象是 Node.js
中用于处理二进制数据的对象
javascript
import fs from 'fs/promises'
import path from "path";
const safePath = path.resolve(import.meta.dirname, './readme.txt')
const buffer = fs.readFile(safePath)
buffer.then(res => {
console.log(res.toString())
})
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
appendFile
如果文件不存在则创建新文件
如果文件存在则追加内容到文件中
javascript
import fs from 'fs/promises'
import path from "path";
const safePath = path.resolve(import.meta.dirname, './readme.txt')
const data = '红红火火恍恍惚惚\n哈哈哈哈哈哈哈哈'
fs.appendFile(safePath, data).then(() => {
console.log('文件创建及写入完成')
}).catch(e => {
console.log('写入错误:', e)
})
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
mkdir
创建目录,路径为新目录路径
javascript
import fs from 'fs/promises'
import path from "path";
const dirPath = path.resolve(import.meta.dirname, './src')
fs.mkdir(dirPath)
.then(() => console.log('创建目录成功'))
.catch(e => console.log('创建目录失败', e))
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
javascript
import fs from 'fs/promises'
import path from "path";
const dirPath = path.resolve(import.meta.dirname, './src/abc')
fs.mkdir(dirPath, {recursive: true})
.then(() => console.log('创建目录成功'))
.catch(e => console.log('创建目录失败', e))
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
rmdir
注意
只可删除空目录,如果非空目录,请使用 rm()
递归删除
javascript
import fs from 'fs/promises'
import path from "path";
const dirPath = path.resolve(import.meta.dirname, './src')
fs.rmdir(dirPath)
.then(() => console.log('删除文件夹成功'))
.catch(e => console.log('删除文件夹失败', e))
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
rm
删除文件或目录
重要提示
递归删除注意了,会删除文件夹下所有所有的内容
javascript
import fs from 'fs/promises'
import path from "path";
const filePath = path.resolve(import.meta.dirname, './fuck.txt')
fs.rm(filePath)
.then(() => console.log('文件删除成功'))
.catch(e => console.log('文件删除失败', e))
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
javascript
import fs from 'fs/promises'
import path from "path";
const filePath = path.resolve(import.meta.dirname, './src')
fs.rm(filePath, {recursive: true})
.then(() => console.log('文件夹及内容删除成功'))
.catch(e => console.log('文件夹及内容删除失败', e))
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
rename
重命名,可重命名文件夹以及文件
javascript
import fs from 'fs/promises'
import path from "path";
const oldDirPath = path.resolve(import.meta.dirname, './src')
const newDirPath = path.resolve(import.meta.dirname, './rsc')
fs.rename(oldDirPath, newDirPath)
.then(() => console.log('文件夹重命名成功'))
.catch(e => console.log('文件夹重命名失败', e))
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
javascript
import fs from 'fs/promises'
import path from "path";
const oldFilePath = path.resolve(import.meta.dirname, './readme.txt')
const newFilePath = path.resolve(import.meta.dirname, './fuck.txt')
fs.rename(oldFilePath, newFilePath)
.then(() => console.log('文件重命名成功'))
.catch(e => console.log('文件重命名失败', e))
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
copyFile
复制文件
javascript
import fs from 'fs/promises'
import path from "path";
const oldFilePath = path.resolve(import.meta.dirname, './fuck.txt')
const newFilePath = path.resolve(import.meta.dirname, './rsc/fuck.txt')
fs.copyFile(oldFilePath, newFilePath)
.then(() => console.log('文件复制成功'))
.catch(e => console.log('文件复制失败', e))
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9