nodejs文件系统模块常用api操作
一、文件常用的api操作:
首先,需要引入文件系统模块:
const fs = require('fs');
1.fs.readFile(path[, options], callback)
作用:异步地读取文件的全部内容。
用法:
注:
1)回调会传入两个参数 (err, data),其中 data 是文件的内容。
2)如果没有指定字符编码,则返回原始的 buffer。
3)如果 options 是字符串,则它指定字符编码:
fs.readFile('/etc/passwd', 'utf8', callback);
例如:
新建一个hello.txt文件:
hello world!
新建一个file_test.js文件,来读取hello.txt文件的内容:
const fs = require('fs');
fs.readFile('./hello.txt',(err,data) => {
if (err) throw err;
console.log(data.toString());
})
运行结果:
2.fs.writeFile(file, data[, options], callback)
当 file 是文件名时,则异步地写入数据到文件(如果文件已存在,则覆盖文件)。 data 可以是字符串或 buffer。
例如:
新建一个file_test.js文件,在hello.txt文件中写入内容‘this is a test.’:
const fs = require('fs');
fs.writeFile('./hello.txt','this is a test.',err => {
if (err) throw err;
console.log('写入成功!');
})
运行结果:
3.fs.appendFile(path, data[, options], callback)
异步地追加数据到文件,如果文件尚不存在则创建文件。 data 可以是字符串或 Buffer。
例如:
新建一个file_test.js文件,在hello.txt文件中追加内容’hello world’:
const fs = require('fs');
const buf = Buffer.from('hello world')
fs.appendFile('./hello.txt',buf,err => {
if (err) throw err;
console.log('追加成功!');
})
运行结果:
3.fs.stat(path[, options], callback)
作用:获取文件信息,判断文件状态(是文件还是文件夹)
例如:
新建一个file_test.js文件,打印出文件的信息:
const fs = require('fs');
fs.stat('./hello.txt',(err,stats) => {
if (err) throw err;
console.log(stats);
})
运行结果:
常用的stats里的两个方法:
- stats.isFile() 判断是否是文件
- stats.isDirectory() 判断是否是文件夹
4.fs.rename(oldPath, newPath, callback)
异步地把 oldPath (旧的文件路径)下的文件重命名为 newPath(新的文件路径)下提供的名字。 如果 newPath 已存在,则覆盖它。
例如:
把./hello.txt的名字改为test.txt:
const fs = require('fs');
fs.rename('./hello.txt','./test.txt',err => {
if (err) throw err;
console.log('重命名文件成功!');
})
运行结果:
5.fs.unlink(path, callback)
作用:异步地删除文件或符号链接。
例如:
删除文件test.txt:
const fs = require('fs');
fs.unlink('./test.txt',err => {
if (err) throw err;
console.log('删除文件成功');
})
二、如何使用文件系统操作文件夹:
1.fs.mkdir(path[, options], callback)
作用:异步地创建目录。
在当前目录下创建a文件夹:
const fs = require('fs');
fs.mkdir('./a',err => {
if(err) throw err;
console.log('创建文件夹成功!');
})
如果想在当前目录下创建一个文件夹b同时文件夹b下创建文件夹c(当前目录下没有b和次文件夹),只把路径写为:’./b/c’是无法成功创建的,会报错。必须用到recursive:true递归创建:
const fs = require('fs');
fs.mkdir('./b/c',{
recursive:true
},err => {
if(err) throw err;
console.log('创建文件夹成功!');
})
2.fs.readdir(path[, options], callback)
作用:读取目录的内容, 回调有两个参数 (err, files),其中 files 是当前目录中文件的名称的数组。
可选的 options 参数可以是字符串(指定字符编码)、或具有 encoding 属性(指定用于传给回调的文件名的字符编码)的对象。 如果 encoding 被设置为 ‘buffer’,则返回的文件名会作为 Buffer 对象传入。
如果 options.withFileTypes 被设置为 true,则 files 数组会包含 fs.Dirent 对象。
例如:
const fs = require('fs');
fs.readdir('./',(err,files) => {
if(err) throw err;
console.log(files)
})
输出结果:
options.withFileTypes 被设置为 true:
const fs = require('fs');
fs.readdir('./',{
withFileTypes:true
},(err,files) => {
if(err) throw err;
console.log(files)
})
输出结果:
3.fs.rmdir(path[, options], callback)
作用:删除文件夹。
const fs = require('fs');
fs.rmdir('./a',err => {
if(err) throw err;
console.log('删除文件夹成功');
})
这样只能删除一个空的文件夹,如果想删除一个非空的文件夹(比如:b文件夹下c文件夹)这就需要使用recursive:true 递归的进行删除,如下:
const fs = require('fs');
fs.rmdir('./b',{
recursive:true
},err => {
if(err) throw err;
console.log('删除文件夹成功');
})
4.fs.watch(filename[, options][, listener])
监视 filename 的更改,其中 filename 是文件或目录。
例如:
const fs = require('fs');
fs.watch('./',(eventType,filename) => {
console.log(eventType,filename);
})
5.监听文件变化 chockidar
1)安装chockidar
在终端输入命令
npm install chokidar --save-dev
2)在文件中引入chokidar
const chokidar = require('chokidar');
3)使用
chokidar.watch('./').on('all',(event,path) =>{
console.log(event,path)
})
设置ignored可以忽略一些文件,如:
chokidar.watch('./',{
ignored:'./node_modules'
}).on('all',(event,path) =>{
console.log(event,path)
})