Node.js文件系統(tǒng)fs擴展fs-extra說明
fs-extra 是fs 的擴展,繼承了 fs 所有方法并為這些方法添加了 promise 語法
安裝
npm i fs-extra
使用
const fse = require('fs-extra')Async 方法
copyemptyDirensureFileensureDirensureLinkensureSymlinkmkdirpmkdirsmoveoutputFileoutputJsonpathExistsreadJsonremovewriteJson
主要介紹異步的相關(guān)方法
方法
方法的具體使用,可以看官方文檔詳細介紹,這里只是簡單介紹一下這些方法的使用和作用
copy()
copy(src:string, dest:string, [options:object, callback:function])
注意:src 如果是目錄,它將復(fù)制此目錄內(nèi)的所有內(nèi)容,而不是整個目錄本身;dest 注意的是 ,如果 src 是文件, dest 則不能是目錄
復(fù)制文件或目錄,目錄可以包含內(nèi)容
// 復(fù)制文件
fse.copy('old.txt', 'new.txt', err => {
? if (err) throw err
? console.log('success')
})
// 復(fù)制目錄,目錄可以包含內(nèi)容
fse.copy('utils', 'newutils', err => {
? if (err) throw err
? console.log('success')
})
// promise?
fse.copy('utils', 'newutils')
.then(() => {
?? ?console.log('success')
})
.catch(err => {
?? ?console.log(err)
})
// async/await
async function test () {
?? ?try {
?? ??? ?await fse.copy('utils', 'newutils')
?? ??? ?console.log('success')
?? ?} catch (err) {
?? ??? ?console.log(err)
?? ?}
}
test()emptyDir()
emptyDir(dir:string, [callback:function])
確保目錄為空,如果目錄不為空,則刪除目錄內(nèi)容。如果該目錄不存在,則創(chuàng)建該目錄。目錄本身不會被刪除
實例
fse.emptyDir('newutils', err => {
? if (err) throw err
? console.log('success')
})ensureFile()
ensureFile(file:string, [callback:func])
確保文件存在,如果請求創(chuàng)建的文件位于不存在的目錄中,則會創(chuàng)建這些目錄。如果該文件已存在,則不進行修改
實例
fse.ensureFile('newutils/seq.js', err => {
? if (err) throw err
? console.log('success')
})ensureDir()
ensureDir(dir:string, [callback:func])
如果目錄結(jié)構(gòu)不存在,則創(chuàng)建它,如果目錄存在,則不進行創(chuàng)建
等同于 mkdirs()、mkdirp()
實例
fse.ensureDir('newutil', err => {
? if (err) throw err
? console.log('success')
})move()
move(src:string, dest:string, [options:object, callback:func])
移動文件或目錄
實例
fse.move('newutils/seq.js', 'newutil/seq.js', err => {
? if (err) throw err
? console.log('success')
})outputFile()
outputFile(file:string, data:string|Buffer|Uint8Array, [options:String|object, callback:func])
寫入文件數(shù)據(jù),如果父級目錄不存在,則創(chuàng)建它。file 必須是文件路徑,不允許使用緩沖區(qū)或文件描述符
實例
fse.outputFile('newutil/seq.js', 'hello, Node.js', err => {
? if (err) throw err
? console.log('success')
})outputJson()
outputJson(file:string, object:object, [options:object, callback:func])
寫入JSON 文件 JSON 數(shù)據(jù),默認 w 模式,會覆蓋文件原有內(nèi)容;如果目錄不存在,會被創(chuàng)建
實例
fse.outputJson('newutil/seq.json', { name: 'duli' }, err => {
? if (err) throw err
? console.log('success')
})writeJson()
writeJson(file, object, [options, callback])
將對象寫入 JSON 文件,幾乎與 outputJson 相同,除了必須保證目錄存在外
實例
fse.writeJSON('newutil/seq.json', { name: 'cc' }, err => {
? if (err) throw err
? console.log('success')
})pathExists()
pathExists(file:string, [, callback:func])
檢查文件系統(tǒng)來測試給定路徑是否存在
實例
fse.pathExists('newutil/seq.js', (err, exists) => {
? if (err) throw err
? console.log(exists)
})readJson()
readJson(file:string, [options:object, callback:func])
讀取 JSON 文件,然后將其解析為對象
實例
fse.readJson('newutil/seq.json', (err, obj) => {
? if (err) throw err
? console.log(obj)
? console.log(typeof obj)
})remove()
remove(path:String, [callback:func])
刪除文件或目錄,該目錄可以包含內(nèi)容
實例
fse.remove('new.txt', err => {
? if (err) throw err
? console.log('success')
})以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
pnpm實現(xiàn)依賴包共享和依賴包項目隔離的方法詳解
pnpm是Node.js的包管理器,它是 npm 的直接替代品,相對于npm和yarn它的優(yōu)點就在于速度快和高效節(jié)省磁盤空間,本文主要講解pnpm相比于npm/yarn如何利用軟硬鏈接來節(jié)省磁盤空間,以及如何實現(xiàn)依賴包共享和依賴包項目隔離的,需要的朋友可以參考下2024-05-05
nodejs中解決異步嵌套循環(huán)和循環(huán)嵌套異步的問題
本篇文章主要介紹了nodejs中解決異步嵌套循環(huán)和循環(huán)嵌套異步的問題,具有一定的參考價值,有興趣的可以了解一下2017-07-07

