97色伦色在线综合视频,无玛专区,18videosex性欧美黑色,日韩黄色电影免费在线观看,国产精品伦理一区二区三区,在线视频欧美日韩,亚洲欧美在线中文字幕不卡

網(wǎng)站項(xiàng)目合同網(wǎng)站建設(shè)的目標(biāo)與期望

鶴壁市浩天電氣有限公司 2026/01/24 05:23:59
網(wǎng)站項(xiàng)目合同,網(wǎng)站建設(shè)的目標(biāo)與期望,三大電商平臺(tái)是哪三個(gè),推廣自己的網(wǎng)站需要怎么做fs.promises 是 Node.js fs 模塊提供的Promise 化文件系統(tǒng) API#xff0c;替代了傳統(tǒng)的回調(diào)式 fs 方法#xff0c;天然適配 async/await 語(yǔ)法#xff0c;是現(xiàn)代 Node.js 開(kāi)發(fā)中處理文件系統(tǒng)的首選方式。 一、核心基礎(chǔ) 1. 兼容性 Node.js v10.0.0 起#xff0c;fs.promis…fs.promises是 Node.jsfs模塊提供的Promise 化文件系統(tǒng) API替代了傳統(tǒng)的回調(diào)式fs方法天然適配async/await語(yǔ)法是現(xiàn)代 Node.js 開(kāi)發(fā)中處理文件系統(tǒng)的首選方式。一、核心基礎(chǔ)1. 兼容性Node.js v10.0.0 起fs.promises被標(biāo)記為穩(wěn)定版Node.js v14.0.0 起支持import fs from fs/promises更簡(jiǎn)潔的導(dǎo)入方式。2. ESM 配置前提使用 ESM 格式import/export需滿足以下任一條件將文件后綴改為.mjs在項(xiàng)目根目錄的package.json中添加type: module。3. 導(dǎo)入方式推薦使用更簡(jiǎn)潔的方式Node.js 14// 推薦直接導(dǎo)入 promises 模塊importfsfromfs/promises;// 配合路徑處理ESM 內(nèi)置模塊importpathfrompath;兼容更低版本Node.js 10import{promisesasfs}fromfs;importpathfrompath;二、核心方法 代碼示例一文件基礎(chǔ)操作1. 讀取文件fs.readFile讀取文件內(nèi)容支持文本/二進(jìn)制格式參數(shù)說(shuō)明path文件路徑推薦用path.resolve處理options可選包含encoding編碼如utf8、flag操作模式如r只讀。示例1讀取文本文件asyncfunctionreadTextFile(){// 絕對(duì)路徑避免相對(duì)路徑陷阱constfilePathpath.resolve(__dirname,example.txt);try{constcontentawaitfs.readFile(filePath,{encoding:utf8,// 文本編碼省略則返回 Bufferflag:r// 只讀模式默認(rèn)});console.log(文件內(nèi)容 ,content);}catch(err){console.error(讀取失敗,err.message);}}readTextFile();示例2讀取二進(jìn)制文件圖片/視頻asyncfunctionreadBinaryFile(){constimgPathpath.resolve(__dirname,image.png);try{// 不指定 encoding返回 Bufferconstbufferawaitfs.readFile(imgPath);console.log(文件大小,buffer.length,字節(jié));// 可直接寫(xiě)入新文件復(fù)制二進(jìn)制文件awaitfs.writeFile(path.resolve(__dirname,copy-image.png),buffer);}catch(err){console.error(讀取二進(jìn)制文件失敗,err.message);}}readBinaryFile();2. 寫(xiě)入文件fs.writeFile寫(xiě)入內(nèi)容到文件默認(rèn)覆蓋原有內(nèi)容參數(shù)說(shuō)明path目標(biāo)文件路徑data寫(xiě)入內(nèi)容字符串/Buffer/Uint8Arrayoptions可選包含encoding、flagw覆蓋、a追加、wx原子寫(xiě)入文件存在則失敗、mode文件權(quán)限如0o644。示例寫(xiě)入/追加文件asyncfunctionwriteFileExample(){constfilePathpath.resolve(__dirname,output.txt);constcontentHello fs.promises! 當(dāng)前時(shí)間${newDate().toISOString()};try{// 1. 覆蓋寫(xiě)入默認(rèn) flag: wawaitfs.writeFile(filePath,content,{encoding:utf8,mode:0o644// 權(quán)限所有者讀寫(xiě)其他只讀});console.log(覆蓋寫(xiě)入成功);// 2. 追加內(nèi)容兩種方式awaitfs.appendFile(filePath, 這是追加的內(nèi)容,{encoding:utf8});// 或用 writeFile flag: a// await fs.writeFile(filePath, 另一種追加方式, { flag: a });}catch(err){console.error(寫(xiě)入失敗,err.message);}}writeFileExample();3. 復(fù)制文件fs.copyFile復(fù)制文件支持原子操作參數(shù)src源文件路徑dest目標(biāo)文件路徑mode可選如fs.constants.COPYFILE_EXCL目標(biāo)存在則報(bào)錯(cuò)。asyncfunctioncopyFileExample(){constsrcpath.resolve(__dirname,source.txt);constdestpath.resolve(__dirname,target.txt);try{// 基礎(chǔ)復(fù)制目標(biāo)存在則覆蓋awaitfs.copyFile(src,dest);console.log(文件復(fù)制成功);// 原子復(fù)制目標(biāo)存在則報(bào)錯(cuò)// await fs.copyFile(src, dest, fs.constants.COPYFILE_EXCL);}catch(err){console.error(復(fù)制失敗,err.message);}}copyFileExample();4. 重命名/移動(dòng)文件fs.rename既可以重命名文件也可以移動(dòng)文件跨目錄。asyncfunctionrenameFileExample(){constoldPathpath.resolve(__dirname,old-name.txt);constnewPathpath.resolve(__dirname,new-name.txt);// 移動(dòng)到子目錄constmovePathpath.resolve(__dirname,subdir/new-name.txt);try{// 1. 重命名awaitfs.rename(oldPath,newPath);console.log(重命名成功);// 2. 移動(dòng)需確保子目錄存在awaitfs.mkdir(path.dirname(movePath),{recursive:true});awaitfs.rename(newPath,movePath);console.log(移動(dòng)文件成功);}catch(err){console.error(重命名/移動(dòng)失敗,err.message);}}renameFileExample();5. 刪除文件fs.unlink / fs.rmfs.unlink傳統(tǒng)刪除文件方法fs.rmNode.js 14更通用支持文件/目錄推薦使用。asyncfunctiondeleteFileExample(){constfilePathpath.resolve(__dirname,to-delete.txt);try{// 推薦fs.rmforce: true 避免文件不存在時(shí)報(bào)錯(cuò)awaitfs.rm(filePath,{force:true});console.log(文件刪除成功);// 傳統(tǒng)方式fs.unlink// await fs.unlink(filePath);}catch(err){console.error(刪除失敗,err.message);}}deleteFileExample();6. 獲取文件信息fs.stat / fs.lstatfs.stat獲取文件/目錄信息fs.lstat區(qū)分符號(hào)鏈接返回鏈接本身信息而非目標(biāo)文件。asyncfunctiongetFileStat(){constfilePathpath.resolve(__dirname,example.txt);try{conststatsawaitfs.stat(filePath);console.log(文件信息,{isFile:stats.isFile(),// 是否是文件isDirectory:stats.isDirectory(),// 是否是目錄size:stats.size,// 大小字節(jié)birthtime:stats.birthtime,// 創(chuàng)建時(shí)間mtime:stats.mtime// 修改時(shí)間});}catch(err){console.error(獲取文件信息失敗,err.message);}}getFileStat();二目錄操作1. 創(chuàng)建目錄fs.mkdir支持創(chuàng)建多級(jí)目錄recursive: true。asyncfunctioncreateDirExample(){// 多級(jí)目錄constdirPathpath.resolve(__dirname,new-dir/sub-dir/child-dir);try{awaitfs.mkdir(dirPath,{recursive:true,// 自動(dòng)創(chuàng)建不存在的父目錄mode:0o755// 目錄權(quán)限所有者讀寫(xiě)執(zhí)行其他讀執(zhí)行});console.log(多級(jí)目錄創(chuàng)建成功);}catch(err){console.error(創(chuàng)建目錄失敗,err.message);}}createDirExample();2. 讀取目錄fs.readdir讀取目錄下的文件/子目錄withFileTypes: true可獲取文件類(lèi)型信息。asyncfunctionreadDirExample(){constdirPathpath.resolve(__dirname,new-dir);try{// withFileTypes: true 返回 Dirent 對(duì)象包含類(lèi)型信息constdirentsawaitfs.readdir(dirPath,{withFileTypes:true});dirents.forEach(dirent{consttypedirent.isFile()?文件:dirent.isDirectory()?目錄:其他;console.log(名稱(chēng)${dirent.name}類(lèi)型${type});});}catch(err){console.error(讀取目錄失敗,err.message);}}readDirExample();3. 刪除目錄fs.rmdir / fs.rmfs.rmdir傳統(tǒng)刪除目錄需空目錄Node.js 12 支持recursive: true刪除非空f(shuō)s.rmNode.js 14推薦支持遞歸刪除非空目錄。asyncfunctiondeleteDirExample(){constdirPathpath.resolve(__dirname,new-dir);try{// 推薦fs.rm 遞歸刪除force: true 避免目錄不存在時(shí)報(bào)錯(cuò)awaitfs.rm(dirPath,{recursive:true,force:true});console.log(目錄刪除成功);// 傳統(tǒng)方式fs.rmdir需 recursive: true// await fs.rmdir(dirPath, { recursive: true });}catch(err){console.error(刪除目錄失敗,err.message);}}deleteDirExample();三其他常用方法1. 檢查文件可訪問(wèn)性fs.access驗(yàn)證文件/目錄的權(quán)限讀/寫(xiě)/執(zhí)行或是否存在。asyncfunctioncheckAccessExample(){constfilePathpath.resolve(__dirname,example.txt);try{// 檢查是否存在fs.constants.F_OKawaitfs.access(filePath,fs.constants.F_OK);console.log(文件存在);// 檢查是否可讀fs.constants.R_OKawaitfs.access(filePath,fs.constants.R_OK);console.log(文件可讀);// 檢查是否可寫(xiě)fs.constants.W_OKawaitfs.access(filePath,fs.constants.W_OK);console.log(文件可寫(xiě));}catch(err){console.error(訪問(wèn)檢查失敗,err.message);}}checkAccessExample();2. 符號(hào)鏈接操作fs.symlink / fs.readlinkasyncfunctionsymlinkExample(){consttargetpath.resolve(__dirname,example.txt);// 目標(biāo)文件constlinkPathpath.resolve(__dirname,example-link.txt);// 鏈接文件try{// 創(chuàng)建符號(hào)鏈接awaitfs.symlink(target,linkPath);console.log(符號(hào)鏈接創(chuàng)建成功);// 讀取鏈接指向的路徑constrealPathawaitfs.readlink(linkPath);console.log(鏈接指向,realPath);}catch(err){console.error(符號(hào)鏈接操作失敗,err.message);}}symlinkExample();三、錯(cuò)誤處理fs.promises所有方法返回的 Promise 失敗時(shí)會(huì)拋出錯(cuò)誤需用try/catch捕獲常見(jiàn)錯(cuò)誤碼錯(cuò)誤碼含義ENOENT文件/目錄不存在EACCES權(quán)限不足EEXIST文件/目錄已存在原子操作時(shí)EPERM操作不被系統(tǒng)允許EISDIR操作對(duì)象是目錄但方法要求文件示例針對(duì)性錯(cuò)誤處理asyncfunctionerrorHandlingExample(){constfilePathpath.resolve(__dirname,non-existent.txt);try{awaitfs.readFile(filePath,utf8);}catch(err){switch(err.code){caseENOENT:console.error(錯(cuò)誤文件不存在);break;caseEACCES:console.error(錯(cuò)誤無(wú)讀取權(quán)限);break;default:console.error(未知錯(cuò)誤,err.message);}}}errorHandlingExample();四、關(guān)鍵注意事項(xiàng)1. ESM 中 __dirname/__filename 替代ESM 中沒(méi)有__dirname/__filename需手動(dòng)實(shí)現(xiàn)import{fileURLToPath}fromurl;import{dirname}frompath;const__filenamefileURLToPath(import.meta.url);// 當(dāng)前文件路徑const__dirnamedirname(__filename);// 當(dāng)前文件目錄2. 大文件處理fs.readFile/fs.writeFile會(huì)將整個(gè)文件加載到內(nèi)存大文件100MB推薦使用流import{createReadStream,createWriteStream}fromfs;import{pipeline}fromstream/promises;// 流的 Promise 化 APIasyncfunctionstreamLargeFile(){constsrcpath.resolve(__dirname,large-file.txt);constdestpath.resolve(__dirname,copy-large-file.txt);try{// 流式復(fù)制大文件awaitpipeline(createReadStream(src),createWriteStream(dest));console.log(大文件復(fù)制成功);}catch(err){console.error(大文件處理失敗,err.message);}}streamLargeFile();3. 批量操作優(yōu)化批量處理文件時(shí)用Promise.all并行執(zhí)行注意系統(tǒng)文件描述符限制asyncfunctionbatchReadFiles(){constfiles[file1.txt,file2.txt,file3.txt].map(fpath.resolve(__dirname,f));try{// 并行讀取失敗時(shí)捕獲單個(gè)錯(cuò)誤不阻塞整體constresultsawaitPromise.all(files.map(filefs.readFile(file,utf8).catch(err({file,error:err.message}))));results.forEach((res,idx){if(res.error){console.error(讀取${files[idx]}失敗,res.error);}else{console.log(讀取${files[idx]}成功,res.substring(0,50)...);}});}catch(err){console.error(批量操作異常,err.message);}}batchReadFiles();4. 原子操作使用flag參數(shù)實(shí)現(xiàn)原子寫(xiě)入避免并發(fā)覆蓋wx寫(xiě)入文件若文件已存在則失敗ax追加內(nèi)容若文件不存在則失敗。asyncfunctionatomicWrite(){constfilePathpath.resolve(__dirname,atomic.txt);try{awaitfs.writeFile(filePath,原子寫(xiě)入內(nèi)容,{flag:wx});console.log(原子寫(xiě)入成功);}catch(err){if(err.codeEEXIST){console.error(文件已存在原子寫(xiě)入失敗避免覆蓋);}}}atomicWrite();五、總結(jié)fs.promises是 Node.js 處理文件系統(tǒng)的現(xiàn)代方案核心優(yōu)勢(shì)擺脫回調(diào)地獄代碼更易讀、易維護(hù)適配async/await錯(cuò)誤處理更清晰API 語(yǔ)義化與傳統(tǒng)fs方法一一對(duì)應(yīng)學(xué)習(xí)成本低。
版權(quán)聲明: 本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)聯(lián)系我們進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

網(wǎng)站各類(lèi)模塊內(nèi)容說(shuō)明背景音樂(lè) wordpress

網(wǎng)站各類(lèi)模塊內(nèi)容說(shuō)明,背景音樂(lè) wordpress,什么是網(wǎng)絡(luò)營(yíng)銷(xiāo)行為分析,網(wǎng)站建設(shè)如何開(kāi)單我的創(chuàng)業(yè)故事#xff1a;《87年出生#xff0c;我開(kāi)了家一人公司#xff0c;年?duì)I收百萬(wàn)》大家好#xff

2026/01/23 01:58:01

網(wǎng)站中加入地圖北京網(wǎng)站制作開(kāi)發(fā)公司

網(wǎng)站中加入地圖,北京網(wǎng)站制作開(kāi)發(fā)公司,中國(guó)建設(shè)銀行網(wǎng)站忘記密碼,視頻網(wǎng)站開(kāi)發(fā)視頻還在為 Android 設(shè)備刷機(jī)煩惱嗎#xff1f;普通 Fastboot 工具功能有限#xff0c;操作繁瑣#xff1

2026/01/23 03:18:01

可以玩小游戲的網(wǎng)站闡述網(wǎng)站建設(shè)的步驟

可以玩小游戲的網(wǎng)站,闡述網(wǎng)站建設(shè)的步驟,棗莊專(zhuān)業(yè)三合一網(wǎng)站開(kāi)發(fā),大渡口的網(wǎng)站開(kāi)發(fā)公司快速體驗(yàn) 打開(kāi) InsCode(快馬)平臺(tái) https://www.inscode.net輸入框內(nèi)輸入如下內(nèi)容#x

2026/01/23 00:26:01

十大網(wǎng)頁(yè)制作工具商城網(wǎng)站怎么優(yōu)化

十大網(wǎng)頁(yè)制作工具,商城網(wǎng)站怎么優(yōu)化,如何在路由器上做網(wǎng)站轉(zhuǎn)跳,平面設(shè)計(jì)常用的軟件多軸控制器USB連不上#xff1f;別急#xff0c;這可能是你忽略的幾個(gè)關(guān)鍵細(xì)節(jié) 在調(diào)試一臺(tái)新的多軸運(yùn)動(dòng)控制器時(shí)#x

2026/01/22 22:31:01