本文共 5105 字,大约阅读时间需要 17 分钟。
FS 模块提供了两种检查文件可访问性的方法:access() 和 accessSync()。
fs.access(path, callback)fs.accessSync(path)
path:指定要检查的文件或目录路径mode(可选):执行检查的操作,默认为检查文件是否存在callback:回调函数,接收错误信息和返回结果fs.open()、fs.readFile() 或 fs.writeFile() 之前使用 fs.access() 检查文件状态。如有必要,建议直接打开或读取文件,处理文件不存在或状态改变的情况。const fs = require('fs');fs.access('./test.js', function (err) { console.log(err ? '不可访问' : '可访问');});fs.accessSync('./text.js'); // 直接报错 FS 模块提供了两种打开文件的方法:open() 和 openSync()。
fs.open(path, flags, mode, callback)fs.openSync(path, flags, mode)
path:目标文件路径flags:文件打开模式(如 r 读取模式、r+ 读写模式等)mode(可选):文件操作模式callback:回调函数,接收文件描述符和错误信息const fs = require('fs');fs.open('./router.js', 'a+', function (err, fd) { if (!err) { console.log(fd); } else { console.log(err); }});fs.openSync('./router.js', 'a+'); FS 模块提供了两种读取文件的方法:readFile() 和 readFileSync()。
fs.readFile(path, encoding, callback)fs.readFileSync(path, encoding)
path:目标文件路径encoding(可选):文件编码格式,默认为 utf-8callback:回调函数,接收读取结果和错误信息readFileSync:同步读取文件的方法fs.readFile('./output.txt', 'utf-8', function (err, data) { console.log('err', err); console.log('data', data);});const fileData = fs.readFileSync('./output.txt', 'utf-8'); FS 模块提供了两种写入文件的方法:writeFile() 和 writeFileSync()。
fs.writeFile(file, data, encoding, callback)fs.writeFileSync(file, data, encoding)
file:目标文件路径或文件描述符data:要写入的数据encoding(可选):文件编码格式,默认为 utf-8callback:回调函数,接收错误信息writeFileSync:同步写入文件的方法const fs = require('fs');fs.writeFile('./op.js', 'const a = "a";', 'utf-8', function (err) { console.log(err); // null});fs.writeFileSync('./op.js', 'const b = "a";', 'utf-8'); FS 模块提供了两种追加数据到文件的方法:appendFile() 和 appendFileSync()。
fs.appendFile(file, data, callback)fs.appendFileSync(file, data)
file:目标文件路径或文件描述符data:要追加的数据callback:回调函数,接收错误信息appendFileSync:同步追加数据的方法const fs = require('fs');fs.appendFile('./test.txt', 'this is append', function (err) { console.log(err ? '失败' : '成功');});fs.appendFileSync('./test.txt', 'this is appendSync'); FS 模块提供了两种删除文件的方法:unlink() 和 unlinkSync()。
fs.unlink(path, callback)fs.unlinkSync(path)
path:要删除的文件路径callback:回调函数,接收错误信息unlinkSync:同步删除文件的方法const fs = require('fs');fs.unlink('./output/output.txt', function (err) { console.log(err);});fs.unlinkSync('./output/output.txt'); FS 模块提供了两种创建目录的方法:mkdir() 和 mkdirSync()。
fs.mkdir(path, callback)fs.mkdirSync(path)
path:要创建的目录路径callback:回调函数,接收错误信息mkdirSync:同步创建目录的方法const fs = require('fs');fs.mkdir('./output', function (err) { console.log(err);});fs.mkdirSync('./output'); // 直接报错,重复创建相同目录 FS 模块提供了两种读取目录的方法:readdir() 和 readdirSync()。
fs.readdir(path, callback)fs.readdirSync(path)
path:目标目录路径callback:回调函数,接收目录文件名数组和错误信息readdirSync:同步读取目录的方法const fs = require('fs');fs.readdir('./', function (err, files) { console.log(err || files);});console.log(fs.readdirSync('./')); FS 模块提供了两种查询文件或目录详细信息的方法:stat() 和 statSync()。
fs.stat(path, callback)fs.statSync(path)
path:目标文件或目录路径callback:回调函数,接收统计信息对象和错误信息statSync:同步查询文件或目录详细信息的方法isFile():是否为文件isDirectory():是否为目录atime、atimeMs:访问时间mtime、mtimeMs:修改时间ctime、ctimeMs:创建时间birthtime、birthtimeMs:生命周期时间fs.readdir('./', function (err, files) { if (!err) { files.forEach((file) => { fs.stat('./' + file, function (error, stats) { if (!error) { console.log(file, 'is FILE', stats.isFile()); console.log(file, 'is DIR', stats.isDirectory()); } }); }); }});const stats = fs.statSync('./'); FS 模块提供了两种移除文件夹的方法:rmdir() 和 rmdirSync()。
fs.rmdir(path, callback)fs.rmdirSync(path)
path:要删除的文件夹路径callback:回调函数,接收错误信息rmdirSync:同步移除文件夹的方法const fs = require('fs');fs.rmdir('./output', function (err) { console.log(err || '成功');});fs.rmdirSync('./output'); FS 模块提供了文件变化监视的方法:watchFile()、unwatchFile() 和 watch()。
watchFile():监听文件变化,触发回调函数unwatchFile():停止监视特定文件或所有文件的变化watch():更高效的文件或目录变化监视方法,支持递归子目录fs.watchFile(filename, options, callback)fs.unwatchFile(filename, callback)fs.watch(filename, options, callback)
filename:要监视的文件或目录路径options:配置选项(如 persistent 和 interval)callback:回调函数,接收事件类型和文件名eventType:事件类型(rename 或 change)const fs = require('fs');fs.watchFile('./test.txt', function (curr, prev) { console.log(curr); console.log('+++++++++ ========= ==+++++++++++'); console.log(prev); fs.unwatchFile('./test.txt'); // 一次修改就停止监听});// 示例二const fs = require('fs');const a = fs.watch('./test.txt', function (eventType, fileName) { console.log(eventType); console.log('====== =++++++++ == ========'); console.log(fileName); this.close(); // 停止监视}); FS 模块提供了创建流的方法:createReadStream() 和 createWriteStream()。
fs.createReadStream(path, options)fs.createWriteStream(path, options)
const fs = require('fs');fs.createReadStream('text.txt');fs.createWriteStream('text.txt'); 通过以上方法,您可以轻松地进行文件操作,满足各种开发需求。
转载地址:http://ybvoz.baihongyu.com/