fs.filehandle.write()方法是文件係統模塊中fs.filehandle類的內置應用程序編程接口,用於將緩衝區中的數據寫入該特定文件。
用法:
const filehandle.write(buffer[, offset[, length[, position]]])
參數:此方法確實采用以下參數:
- buffer:緩衝區將提供需要存儲的數據。
- offset:緩衝區中的起點。
- length:要寫入的字節數。
- position:文件的起點。
返回值:此方法返回一個未決的Promise,其中包含buffer和bytesWritten屬性,該屬性描述寫入的字節數。
以下示例程序旨在說明Node.js中fs.filehandle.write()方法的使用:
範例1: 文件名:index.js
// Node.js program to demonstrate the
// filehandle.write() method
const fs = require('fs');
const fsPromises = fs.promises;
console.log("file before write operation:- "
+ fs.readFileSync('example.txt', 'utf8'));
// Initiating asyncrionise function
async function funct() {
// Initializng following variables
let filehandle = null;
let prom = null;
let buffer = Buffer.from('Geeks');
try {
// Creating and initiating filehandle
filehandle = await
fsPromises.open('example.txt', 'r+');
// Writting the file by using
// write() method
prom = filehandle.write(
buffer, 0, buffer.length, 0);
} finally {
if (filehandle) {
// Display the result
prom.then(function (result) {
console.log(
"file after write operation:- "
+ (result.buffer).toString());
})
// Close the file if it is opened.
await filehandle.close();
}
}
}
funct().catch(console.error);
運行程序之前的目錄結構:
運行程序後的目錄結構:
使用以下命令運行index.js文件:
node index.js
輸出:
file before write operation:- Geeks for Geeks file after write operation:- Geeks
範例2: 文件名:index.js
// Node.js program to demonstrate the
// filehandle.sync() method
const fs = require('fs');
const fsPromises = fs.promises;
// Data for the new file
let data = "This is a file containing"
+ " a collection of books.";
// Name of the file to be created
let file = "books.txt";
// Creating the new file 'books.txt'
fs.writeFile(file, data, (err) => {
// Cathing error
if (err) {
console.log(err);
}
});
// Using fs.exists() method
fs.exists(file, (exists) => {
if (exists) {
console.log(
"content of file before operation:- "
+ (fs.readFileSync(file)));
}
});
// Initiating asyncrionise function
async function funct() {
// Initializng filehandle
let filehandle = null;
// Initializng following variables
let buffer = Buffer.from('ABCD');
try {
// Creating and initiating filehandle
filehandle = await
fsPromises.open(file, 'r+');
// Writting the file
// by using write() method
prom = filehandle.write(
buffer, 0, buffer.length, 0);
} finally {
if (filehandle) {
// Display the result
prom.then(function (result) {
console.log(
"file after write operation:- "
+ (result.buffer).toString());
})
await filehandle.close();
}
}
}
funct().catch(console.error);
運行程序之前的目錄結構:
運行程序後的目錄結構:
使用以下命令運行index.js文件:
node index.js
輸出:
content of file before operation:- This is a file containing a collection of books. file after write operation:- ABCD
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js GM resize()用法及代碼示例
- Node.js GM gamma()用法及代碼示例
- Node.js GM spread()用法及代碼示例
- Node.js GM transparent()用法及代碼示例
- Node.js GM write()用法及代碼示例
- Node.js GM negative()用法及代碼示例
- Node.js GM median()用法及代碼示例
- Node.js GM shave()用法及代碼示例
- Node.js GM emboss()用法及代碼示例
- Node.js GM despeckle()用法及代碼示例
- Node.js GM gaussian()用法及代碼示例
- Node.js GM magnify()用法及代碼示例
- Node.js GM minify()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js fs.filehandle.write() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。