#NodeJS# 文件操作模塊API使用

文件操作相關API

  • 引入文件系統模塊fs
const fs = require('fs')
  • fs.readFile(path[, options], callback) 讀取文件
    • path | | 文件路路徑
    • callback 回調函數
      • err
      • data | 讀取的數據
fs.readFile('./hello.txt','utf8',(err,data) => {
if(err) throw err;
console.log(data);
})
  • s.writeFile(file, data[, options], callback) 寫入文件
    • file | | | 文件名或文件描述符。
    • data | | | 寫入的數據
    • options |
      • encoding | 寫入字符串串的編碼格式 默認值: ‘utf8’ 。
      • mode 文件模式(權限) 默認值: 0o666 。
      • flag 參閱⽀支持的文件系統標誌。默認值: ‘w’ 。
    • callback 回調函數
      err
fs.writeFile('./hello.txt','this is a test',err => {
if(err) throw err;
console.log('寫⼊入成功');
})
  • fs.appendFile(path, data[, options], callback) 追加數據到文件
    • path | | | 文件名或文件描述符。
    • data | 追加的數據
    • options |·
      • encoding | 寫入字符串串的編碼格式 默認值: ‘utf8’ 。
      • mode 文件模式(權限) 默認值: 0o666 。
      • flag 參閱支持的文件系統標誌。默認值: ‘a’
    • callback 回調函數
      • err
const buf = Buffer.from('hello world!')
fs.appendFile('./hello.txt',buf,(err) => {
if(err) throw err;
console.log('追加成功');
})
  • fs.stat(path[, options], callback) 獲取⽂文件信息,判斷⽂文件狀態(是⽂文件還是文件夾)
    • path | |
    • options
      • bigint 返回的 fs.Stats 對象中的數值是否應爲 bigint 型。默認值: false
    • callback
      • err
      • stats <fs.Stats> 文件信息
fs.stat('./hello.txtt',(err,stats) => {
if(err){
console.log('文件不不存在');
return;
}
console.log(stats);
console.log(stats.isFile());
console.log(stats.isDirectory());
})
  • fs.rename(oldPath, newPath, callback) 重命名文件
    • oldPath | | 舊⽂文件路路徑名字
    • newPath | | 新⽂文件路路徑名字
    • callback 回調函數
      • err
fs.rename('./hello.txt','./test.txt',err => {
if(err) throw err;
console.log('重命名成功');
})
  • fs.unlink(path, callback) 刪除⽂文件
    • path | |
    • callback
      • err
fs.unlink('./test.txt',err => {
if(err) throw err;
console.log('刪除成功');
})

文件夾操作相關API

  • fs.mkdir(path[, options], callback) 創建⽂文件夾
    • path | |
    • options |
      • recursive 是否遞歸創建 默認值: false 。
      • mode 文件模式(權限)Windows 上不不支持。默認值: 0o777 。
      • callback
        • err
const fs =require('fs');
// fs.mkdir('./a',err => {
// if(err) throw err;
// console.log('創建文件夾成功');
// })
fs.mkdir('./b/c',{
recursive:true
},err => {
if(err) throw err;
console.log('創建文件夾成功');
})
  • fs.readdir (path[, options], callback) 讀取文件夾
    • path | |
    • options |
      • encoding 默認值: ‘utf8’ 。
      • withFileTypes 默認值: false
    • callback
      • err
      • files <string[]> | <buffer[]> | <fs.Dirent[]>
fs.readdir('./',{
encoding:'buffer', //設置buffer,files返回文件名爲buffer對象
withFileTypes:true //單上文件類型
},(err,files) => {
if(err) throw err;
console.log(files)
})
  • fs.rmdir(path[, options], callback) 刪除⽂文件夾
    • path | |
    • options
      • maxRetries 重試次數。出現這類錯誤 EBUSY 、 EMFILE 、 ENFILE 、 ENOTEMPTY 或
        者EPERM ,每⼀一個重試會根據設置的重試間隔重試操作。如果 recursive不不爲true則忽略略. 默認值: 0。
      • retryDelay 重試的間隔,如果 recursive 不不爲true則忽略略. 默認值: 100 。
      • recursive 如果爲 true ,則執⾏行行遞歸的⽬目錄刪除。在遞歸模式中,如果 path 不不存在則不不報告錯誤,並且在失敗時重試操作。默認值: false 。
fs.rmdir('./b',{
recursive:true
},err => {
if(err) throw err;
console.log('刪除⽂文件夾成功');
})
  • 監聽⽂文件變化 chokidar
    • 安裝chokidar
npm install chokidar --save-dev
- Chokidar.watch(path,[options])
chokidar.watch('./',{
ignored:'./node_modules'
}).on('all',(event,path) => {
console.log(event,path)
})

study458

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章