1. <nobr id="easjo"><address id="easjo"></address></nobr>

      <track id="easjo"><source id="easjo"></source></track>
      1. 
        

      2. <bdo id="easjo"><optgroup id="easjo"></optgroup></bdo>
      3. <track id="easjo"><source id="easjo"><em id="easjo"></em></source></track><option id="easjo"><span id="easjo"><em id="easjo"></em></span></option>
          貴州做網站公司
          貴州做網站公司~專業!靠譜!
          10年網站模板開發經驗,熟悉國內外開源網站程序,包括DEDECMS,WordPress,ZBlog,Discuz! 等網站程序,可為您提供網站建設,網站克隆,仿站,網頁設計,網站制作,網站推廣優化等服務。我們專注高端營銷型網站,企業官網,集團官網,自適應網站,手機網站,網絡營銷,網站優化,網站服務器環境搭建以及托管運維等。為客戶提供一站式網站解決方案?。?!

          Node.js fs.copyFile實例講解

          來源:互聯網轉載 時間:2024-01-29 08:30:33

          fs.copyFile()方法用于將文件從源路徑異步復制到目標路徑。默認情況下,Node.js將覆蓋文件(如果文件已存在于給定的目的地)??蛇x的mode參數可用于修改復制操作的行為。

          用法:

          fs.copyFile( src, dest, mode, callback )

          參數:該方法接受上述和以下描述的三個參數:



          • src:它是一個字符串,緩沖區或URL,表示要復制的源文件名。
          • dest:它是一個字符串,緩沖區或URL,表示復制操作將創建的目標文件名。
          • mode:它是一個整數,指定復制操作的行為??梢詾檫@些值提供預定義的常量,這些常量具有各自的行為:
            • fs.constants.COPYFILE_EXCL:該常數指定如果目標文件名已經存在,則復制操作將失敗。
            • fs.constants.COPYFILE_FICLONE:該常數指定復制操作將嘗試創建copy-on-write reflink。如果平臺不支持copy-on-write,則使用回退機制。
            • fs.constants.COPYFILE_FICLONE_FORCE:該常數指定復制操作將嘗試創建copy-on-write reflink。如果平臺不支持copy-on-write,則失敗將失敗,與之前的平臺不同。
            • 這些常量也可以與按位或組合以創建多個值的掩碼。它是一個可選參數。該參數的默認值為0。

          • callback:該方法執行時將調用該函數。
            • err:如果方法失敗,將引發錯誤。

          以下示例說明了Node.js中的fs.copyFile()方法:

          范例1:本示例顯示了“example_file.txt”文件到“copied_file.txt”文件的復制操作。

          // Node.js program to demonstrate the // fs.copyFile() method ??// Import the filesystem module const fs = require('fs'); ??// Get the current filenames // before the function getCurrentFilenames(); console.log("\nFile Contents of example_file:", ??fs.readFileSync("example_file.txt", "utf8")); ??// Copying the file to a the same name fs.copyFile("example_file.txt", "copied_file.txt", (err) => { ??if (err) { ????console.log("Error Found:", err); ??} ??else { ??????// Get the current filenames ????// after the function ????getCurrentFilenames(); ????console.log("\nFile Contents of copied_file:", ??????fs.readFileSync("copied_file.txt", "utf8")); ??} }); ??// Function to get current filenames // in directory function getCurrentFilenames() { ??console.log("\nCurrent filenames:"); ??fs.readdirSync(__dirname).forEach(file => { ????console.log(file); ??}); }

          輸出:

          Current filenames:example_file.txtindex.jsFile Contents of example_file:This is a test file.Current filenames:copied_file.txtexample_file.txtindex.jsFile Contents of copied_file:This is a test file.

          范例2:本示例說明了目標已存在時復制操作失敗。

          // Node.js program to demonstrate the // fs.copyFile() method ??// Import the filesystem module const fs = require('fs'); ??// Get the current filenames // before the function getCurrentFilenames(); console.log("\nFile Contents of example_file:",? ??????fs.readFileSync("example_file.txt", "utf8")); ??// Copying the file to a the same name fs.copyFile("example_file.txt", "copied_file.txt",? ??????fs.constants.COPYFILE_EXCL, (err) => { ??if (err) { ????console.log("Error Found:", err); ??} ??else { ??????// Get the current filenames ????// after the function ????getCurrentFilenames(); ????console.log("\nFile Contents of copied_file:", ??????fs.readFileSync("copied_file.txt", "utf8")); ??} }); ??// Function to get current filenames // in directory function getCurrentFilenames() { ??console.log("\nCurrent filenames:"); ??fs.readdirSync(__dirname).forEach(file => { ????console.log(file); ??}); }

          輸出:

          Current filenames:copied_file.txtexample_file.txtindex.jsFile Contents of example_file:This is a test file.Error:[Error:EEXIST:file already exists, copyfile   'G:\tutorials\nodejs-fs-copyFile\example_file.txt' ->   'G:\tutorials\nodejs-fs-copyFile\copied_file.txt'] {  errno:-4075,  code:'EEXIST',  syscall:'copyfile',  path:'G:\\tutorials\\nodejs-fs-copyFile\\example_file.txt',  dest:'G:\\tutorials\\nodejs-fs-copyFile\\copied_file.txt'}

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




          標簽:copyfile-

          網絡推廣與網站優化公司(網絡優化與推廣專家)作為數字營銷領域的核心服務提供方,其價值在于通過技術手段與策略規劃幫助企業提升線上曝光度、用戶轉化率及品牌影響力。這...

          在當今數字化時代,公司網站已成為企業展示形象、傳遞信息和開展業務的重要平臺。然而,對于許多公司來說,網站建設的價格是一個關鍵考量因素。本文將圍繞“公司網站建設價...

          在當今的數字化時代,企業網站已成為企業展示形象、吸引客戶和開展業務的重要平臺。然而,對于許多中小企業來說,高昂的網站建設費用可能會成為其發展的瓶頸。幸運的是,隨...

          南平最大企業?南平最大的企業福建圣農發展有限公司成立于1999年12月。是一家自營和自宰白羽肉雞的專業生產企業,主要從事肉雞養殖、肉雞屠宰加工和雞肉銷售。公司的前身是福建省光澤縣光大肉雞有限公司;2003年9月4日更名為#34,福建省光澤縣圣農發展有限公司# 34;2003年11月24日更名為#34福建圣農發展有限公司# 34;2006年10月17日,整體變更成立為福建圣農發展有限公司;2009年...

          奧運向前沖闖關最快紀錄?傅洋2008年,傅洋參加湖南衛視《奧運向前沖》節目。因為他的不敗紀錄,他穿著超人服裝被全國觀眾記住,然后參加了幾個電視節目。湖南衛視奧運向前沖,主持人是誰?姓名:王歡,身高:1.66米,體重:51公斤。民族:滿族,出生日期:2月14日,血型:AB。星座:水瓶座,學歷:本科,出生地:河北省承德市。愛好:安靜的時候在家看自己喜歡的書?;顒拥臅r候和好朋友一起吃飯,k歌。去商場和書...

          虛擬pdf打印機是什么?win7可以安裝pdf打印機嗎?虛擬pdf打印機是安裝在計算機上的一種驅動程序,如pdfFactory,它提供了一種比其他程序更簡單、更高效、更廉價的創建PDF文件的解決方案。它可以將單頁或多頁文檔直接打印為PDF文件。PdfFactory支持許多計算機系統,包括Windows 10、8、7、Vista、XP sp2或更高版本??梢?,下載福鑫閱讀器,可以同步安裝虛擬打印機。...

          TOP
          国产初高中生视频在线观看|亚洲一区中文|久久亚洲欧美国产精品|黄色网站入口免费进人
          1. <nobr id="easjo"><address id="easjo"></address></nobr>

              <track id="easjo"><source id="easjo"></source></track>
              1. 
                

              2. <bdo id="easjo"><optgroup id="easjo"></optgroup></bdo>
              3. <track id="easjo"><source id="easjo"><em id="easjo"></em></source></track><option id="easjo"><span id="easjo"><em id="easjo"></em></span></option>