fs.filehandle.fd()方法是文件系统模块中fs.filehandle类的内置应用程序编程接口,用于提供此文件句柄对象的数字文件描述符。
用法:
const filehandle.fd()
参数:此方法不接受任何参数。
返回值:此方法返回此文件句柄对象的数字文件描述符。
以下示例程序旨在说明Node.js中fs.filehandle.fd()方法的使用:
范例1: 文件名:index.js
// Node.js program to demonstrate the
// filehandle.fd() method
const fs = require('fs');
const fsPromises = fs.promises;
console.log("content of file before operation:- "
+ (fs.readFileSync('example.txt')));
// Initiating asyncrionise function
async function funct() {
// Initializng filehandle
let filehandle = null;
let value = null;
try {
// Creating and initiating filehandle
filehandle = await
fsPromises.open('example.txt', 'r+');
// Getting the Numeric file descriptor
// by using fd api
value = filehandle.fd;
} finally {
if (filehandle) {
// Close the file if it is opened.
console.log(
"Numeric file descriptor:- " + value);
console.log(
"content of file after operation:- " +
(fs.readFileSync('example.txt')));
await filehandle.close();
}
}
}
funct().catch(console.error);
运行程序之前的目录结构:
运行程序后的目录结构:
使用以下命令运行index.js文件:
node index.js
输出:
content of file before operation:- ABCD Numeric file descriptor:- 3 content of file after operation:- ABCD
范例2:
文件名:index.js
// Node.js program to demonstrate the
// filehandle.fd() 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;
try {
// Creating and initiating filehandle
filehandle = await
fsPromises.open(file, 'r+');
// Getting the Numeric file descriptor
// by using fd api
value = filehandle.fd;
} finally {
if (filehandle) {
// Close the file if it is opened.
console.log(
"Numeric file descriptor:- "
+ value);
console.log("content of file "
+ "after operation:- "
+ (fs.readFileSync(file)));
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. Numeric file descriptor:- 4 content of file after operation:- This is a file containing a collection of books.
参考: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_filehandle_fd
相关用法
- Node.js console.timeLog()用法及代码示例
- Node.js GM operator()用法及代码示例
- Node.js GM whiteThreshold()用法及代码示例
- Node.js GM whitePoint()用法及代码示例
- Node.js GM write()用法及代码示例
- Node.js GM drawLine()用法及代码示例
- Node.js GM drawEllipse()用法及代码示例
- Node.js GM drawPolygon()用法及代码示例
- Node.js GM drawBezier()用法及代码示例
- Node.js GM drawArc()用法及代码示例
- Node.js GM drawPolyline()用法及代码示例
- Node.js GM drawCircle()用法及代码示例
- Node.js GM channel()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Node.js fs.filehandle.fd() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。