Node.js中“文件係統”模塊的fs.promises.appendFile()方法用於與用戶計算機的硬盤進行交互。 appendFile()方法用於將新數據附加到現有文件中,或者如果該文件不存在,則首先創建文件,然後將給定數據附加到該文件中。 fs.promises.appendFile()方法返回已解決或拒絕的承諾,因此避免了fs.appendFile()方法中可能發生的回調嵌套或回調地獄問題。
用法:
fs.promises.appendFile( path, data, options )
參數:此方法接受三個參數路徑,數據和選項。選項是可選參數。
- path:它是一個字符串,緩衝區或URL,它指定要在其中添加給定數據的目標文件的路徑。
- data:它是一個字符串或緩衝區,將附加到目標文件。
- options:這是一個可選參數,它會以某種方式影響輸出,因此我們是否將其提供給函數調用。
- encoding:它指定了編碼技術,默認值為“ UTF8”。
- mode:它指定文件模式。文件模式使我們可以創建,讀取,寫入或修改文件。默認值為“ 0o666”。
- flag:它指定附加到文件時使用的標誌。默認值為‘a’。
返回值:它返回已解決或已拒絕的承諾。如果成功將數據附加到目標文件,則將解決諾言;否則,如果發生任何錯誤(example-specified文件沒有寫權限等),將被錯誤對象拒絕。
範例1:
// Importing File System module
const fs = require('fs')
// The readFile() method reads the file
// and returns buffer form of the data
fs.promises.readFile('./test.txt')
.then(buff => {
// File content before append
const oldContent = buff.toString()
console.log(`Before Append:${oldContent}\n`)
// Append operation
return fs.promises.appendFile('./test.txt',
'\nHey, I am newly added..!!')
})
.then(() => {
// Getting new file content
return fs.promises.readFile('./test.txt')
})
.then(buff => {
// File content after append
const newContent = buff.toString()
console.log(`After Append:${newContent}\n`)
})
.catch(err => {
console.log(err)
})
我們可以使用async-await關鍵字實現相同的函數。
// Importing File System module
const fs = require('fs')
const appendDataToFile = async (path, data) => {
// The readFile() method reads the file
// and returns buffer form of the data
const oldBuffer = await fs.promises.readFile(path)
// File content before append
const oldContent = oldBuffer.toString()
// Append operation
await fs.promises.appendFile(path, data)
const newBuffer = await fs.promises.readFile(path)
// File content after append
const newContent = newBuffer.toString()
console.log(`Before Append:${oldContent}\n`)
console.log(`After Append:${newContent}`)
}
appendDataToFile('./test.txt',
'\nHey, I am newly added..!!')
.catch(err => {
console.log(err)
})
輸出:
範例2:文件名的給定路徑不存在時。
// Importing File System module
const fs = require('fs')
// Append operation
// If given file does not exist
// it will be created first then
// data is appended
fs.promises.appendFile('./test.txt',
'Please add me to the test file..!!')
.then(() => {
// readFile() method reads the file
// and returns buffer form of the data
return fs.promises.readFile('./test.txt')
})
.then(buff => {
// Appended data
const content = buff.toString()
console.log(`Content:${content}`)
})
.catch(err => {
console.log(err)
})
使用async-await關鍵字實現相同的函數。
// Importing File System module
const fs = require('fs')
const appendDataToFile = async (path, data) => {
// Append operation
// If given file does not exist
// It will created first then
// data is appended
await fs.promises.appendFile(path, data)
// readFile() method reads the file
// and returns buffer form of the data
const buff = await fs.promises.readFile(path)
// File content after append
const content = buff.toString()
console.log(`Content:${content}`)
}
appendDataToFile('./test.txt',
'Please add me to the test file..!!')
.catch(err => {
console.log(err)
})
運行程序之前的目錄結構:
運行程序後的目錄結構:
輸出:
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js GM chop()用法及代碼示例
- Node.js GM drawArc()用法及代碼示例
- Node.js GM edge()用法及代碼示例
- Node.js GM drawLine()用法及代碼示例
- Node.js GM bordercolor()用法及代碼示例
- Node.js GM border()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM channel()用法及代碼示例
- Node.js GM whitePoint()用法及代碼示例
- Node.js GM operator()用法及代碼示例
- Node.js GM transparent()用法及代碼示例
- Node.js GM drawRectangle()用法及代碼示例
注:本文由純淨天空篩選整理自hunter__js大神的英文原創作品 Node.js | fs.promises.appendFile() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。