當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js fs.promises.link()用法及代碼示例

fs.promises.link()方法是fs.promises類的內置應用程序編程接口,用於為文件命名。

用法:

fs.promises.link(existing_path, new_path);

參數:此方法接受上麵提到並在下麵描述的兩個參數:

  • existing_path:它是必需的參數,它指定一個字符串/URL /Buffer,它表示文件的現有路徑。
  • new_path:這是必需的參數,它指定一個字符串/URL /Buffer,它表示文件的新路徑。

返回值:它返回一個承諾。如果鏈接成功,則承諾將沒有任何價值,否則將被錯誤對象拒絕。

以下示例說明了Node.js中fs.promises.link()方法的使用:



範例1:

Javascript

// Accessing fs module 
const fs = require('fs'); 
const fsPromises = fs.promises; 
  
// fs.promises link  
fsPromises.link('./filename.txt','../filename.txt') 
  .then(() => console.log('linked successfully')) 
  .catch(() => console.error('failed to link!'));

輸出

(node:5052) ExperimentalWarning:The fs.promises API is experimental
linked successfully

例子2

Javascript

// Accessing fs module 
const { 
  promises:fsPromises 
} = require('fs'); 
  
// Not work for directory 
// fs.promises link  
fsPromises.link('../dir_name','./dir_name') 
  .then(() => console.log('linked successfully')) 
  .catch(() => console.error('failed to link!'));

輸出

(node:11936) ExperimentalWarning:The fs.promises API is experimental
failed to link!

注意:以上程序將使用node filename.js命令並正確使用file_path。

參考: https://nodejs.org/api/fs.html#fs_fspromises_link_existingpath_newpath

相關用法


注:本文由純淨天空篩選整理自anwesha0107大神的英文原創作品 Node.js fs.promises.link() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。