fs.readlink()方法是fs模塊的內置應用程序編程接口,用於異步返回符號鏈接的值,即它鏈接到的路徑。可選參數可用於指定鏈接路徑的字符編碼。
用法:
fs.readlink( path[, options], callback )
參數:此方法接受上述和以下所述的三個參數:
- path:它是一個字符串,Buffer或URL,它們代表符號鏈接的路徑。
- options:它是一個對象或字符串,可用於指定將影響輸出的可選參數。它具有一個可選參數:
- encoding:它是一個字符串值,它指定返回鏈接路徑的字符編碼。默認值為“ utf8”。
- callback:執行該方法時將調用該函數。
- err:如果方法失敗,將引發錯誤。
- linkString:是包含符號鏈接的字符串值的字符串或Buffer對象。
以下示例說明了Node.js中的fs.readlink()方法:
範例1:本示例讀取文件的symlink值,並更改該值的編碼。
javascript
// Node.js program to demonstrate the
// fs.readlink() method
// Import the filesystem module
const fs = require('fs');
// Create a symbolic link
fs.symlinkSync(__dirname + "\\example_file.txt",
"symlinkToFile", 'file');
console.log("\nSymlink created\n");
// Using the default utf-8 encoding for the link value
fs.readlink("symlinkToFile", (err, linkString) => {
if (err)
console.log(err);
else
console.log("Path of the symlink:", linkString);
});
// Using the base64 encoding for the link value
fs.readlink("symlinkToFile",
// Specify the options object
{encoding:"base64"},
(err, linkString) => {
if (err)
console.log(err);
else
console.log("Path in base64:", linkString);
});
輸出:
Symlink created
Path of the symlink:G:\tutorials\nodejs-fs-readlink\example_file.txt
Path in base64:RzpcdHV0b3JpYWxzXG5vZGVqcy1mcy1yZWFkbGlua1xleGFtcGxlX2ZpbGUudHh0
範例2:本示例讀取目錄的符號鏈接的值。
javascript
// Node.js program to demonstrate the
// fs.readlink() method
// Import the filesystem module
const fs = require('fs');
// Create a symbolic link
fs.symlinkSync(__dirname + "\\example_directory",
"symlinkToDir", 'dir');
console.log("\nSymlink created\n");
fs.readlink("symlinkToDir", (err, linkString) => {
if (err)
console.log(err);
else
console.log("Path of the symlink:", linkString);
});
輸出:
Symlink created
Path of the symlink:G:\tutorials\nodejs-fs-readlink\example_directory
參考:https://nodejs.org/api/fs.html#fs_fs_readlink_path_options_callback
相關用法
- Node.js console.timeLog()用法及代碼示例
- Node.js GM drawPolygon()用法及代碼示例
- Node.js GM scale()用法及代碼示例
- Node.js GM contrast()用法及代碼示例
- Node.js GM lower()用法及代碼示例
- Node.js GM median()用法及代碼示例
- Node.js GM despeckle()用法及代碼示例
- Node.js GM sepia()用法及代碼示例
- Node.js GM paint()用法及代碼示例
- Node.js GM orderedDither()用法及代碼示例
- Node.js GM roll()用法及代碼示例
- Node.js GM operator()用法及代碼示例
- Node.js GM negative()用法及代碼示例
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Node.js fs.readlink() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。