fs.copyFile()方法用于將文件從源路徑異步復制到目標路徑。默認情況下,Node.js將覆蓋文件(如果文件已存在于給定的目的地)??蛇x的mode參數可用于修改復制操作的行為。
用法:
fs.copyFile( src, dest, mode, callback )
參數:該方法接受上述和以下描述的三個參數:
這些常量也可以與按位或組合以創建多個值的掩碼。它是一個可選參數。該參數的默認值為0。
以下示例說明了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
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
網絡推廣與網站優化公司(網絡優化與推廣專家)作為數字營銷領域的核心服務提供方,其價值在于通過技術手段與策略規劃幫助企業提升線上曝光度、用戶轉化率及品牌影響力。這...
在當今數字化時代,公司網站已成為企業展示形象、傳遞信息和開展業務的重要平臺。然而,對于許多公司來說,網站建設的價格是一個關鍵考量因素。本文將圍繞“公司網站建設價...
在當今的數字化時代,企業網站已成為企業展示形象、吸引客戶和開展業務的重要平臺。然而,對于許多中小企業來說,高昂的網站建設費用可能會成為其發展的瓶頸。幸運的是,隨...
南平最大企業?南平最大的企業福建圣農發展有限公司成立于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或更高版本??梢?,下載福鑫閱讀器,可以同步安裝虛擬打印機。...