本篇文章帶大家了解一下node中的fs文件系統(tǒng)模塊和path路徑模塊,希望對(duì)大家有所幫助!
fs文件系統(tǒng)模塊
fs 模塊是 Node.js 官方提供的、用來操作文件的模塊。它提供了一系列的方法和屬性,用來滿足用戶對(duì)文件的操作需求。
- fs.readFile() 方法,用來讀取指定文件中的內(nèi)容
- fs.writeFile() 方法,用來向指定的文件中寫入內(nèi)容 如果要在 JavaScript 代碼中,使用 fs 模塊來操作文件,則需要使用如下的方式先導(dǎo)入它:
const fs = reuire('fs')
讀取指定文件中的內(nèi)容
1. fs.readFile()的語法格式
使用 fs.readFile() 方法,可以讀取指定文件中的內(nèi)容,語法格式如下:
fs.readFile(path[, options], callback)
- 參數(shù)1:必選參數(shù),需要指定一個(gè)文件路徑的字符串,表示要讀取哪個(gè)路徑對(duì)應(yīng)的文件。
- 參數(shù)2:可選參數(shù),表示以什么編碼格式來讀取文件。
- 參數(shù)3:必選參數(shù),文件讀取完成后,通過回調(diào)函數(shù)拿到讀取的結(jié)果。
2. fs.readFile()的示例代碼
以 utf8 的格式,讀取指定文件的內(nèi)容,并打印err 和 data 的值:
const fs = require('fs'); fs.readFile('hello.txt', 'utf-8', (err, data) => { // 判斷是否讀取成功 if (err) return console.log(err); console.log(data); });
向指定的文件中寫入內(nèi)容
1. fs.writeFile()的語法格式
使用 fs.writeFile() 方法,可以向指定的文件中寫入內(nèi)容,語法格式如下:
fs.writeFile(file, data[, options], callback)
- 參數(shù)1:必選參數(shù),需要指定一個(gè)文件路徑的字符串,表示要文件的存放路徑。
- 參數(shù)2:必選參數(shù),表示要寫入的內(nèi)容。
- 參數(shù)3:可選參數(shù),表示以什么格式寫入文件內(nèi)容,默認(rèn)值是utf8。
- 參數(shù)4:必選參數(shù),文件寫入完成后的回調(diào)函數(shù)。
2. fs.writeFile()的示例代碼
const fs = require('fs'); fs.writeFile('./hello.txt', 'hello node', (err) => { // 判斷是否寫入成功 if (err) return console.log(err); console.log('寫入成功'); });
讀取指定目錄下所有文件的名稱
1.fs.readdir()的語法格式
使用 fs.readdir() 方法,可以讀取指定目錄下所有文件的名稱,語法格式如下:
fs.readdir(path[, options], callback)
- 參數(shù)1:必選參數(shù),表示要讀取哪個(gè)目錄下的文件名稱列表。
- 參數(shù)2:可選參數(shù),以什么格式讀取目錄下的文件名稱,默認(rèn)值是utf8。
- 參數(shù)3:必選參數(shù),讀取完成以后的回調(diào)函數(shù)。
2.fs.readdir()的示例代碼
通過 fs.readdir() 方法,可以讀取指定目錄下,所有文件的名稱:
const fs = require('fs'); fs.readdir('./', (err, data) => { // 錯(cuò)誤處理 if (err) return console.log(err); console.log(data); });
fs 模塊-路徑動(dòng)態(tài)拼接的問題
在使用 fs 模塊操作文件時(shí),如果提供的操作路徑是以./ 或 ../ 開頭的相對(duì)路徑時(shí),很容易出現(xiàn)路徑動(dòng)態(tài)拼接錯(cuò)誤的問題。 這是因?yàn)榇a在運(yùn)行的時(shí)候,會(huì)以執(zhí)行node 命令時(shí)所處的目錄,動(dòng)態(tài)拼接出被操作文件的完整路徑。
解決方案:在使用fs 模塊操作文件時(shí),直接提供絕對(duì)路徑,不要提供./ 或 ../ 開頭的相對(duì)路徑,從而防止路徑動(dòng)態(tài)拼接的問題。
注意:使用__dirname 獲取當(dāng)前文件所在的絕對(duì)路徑
const fs = require('fs'); // 拼接要讀取文件的絕對(duì)路徑 let filepath = __dirname +'/hello.txt' fs.readFile(filepath, 'utf-8', (err, data) => { // 判斷是否讀取成功 if (err) return console.log(err); console.log(data); });
path路徑模塊
path 模塊是 Node.js 官方提供的、用來處理路徑的模塊。它提供了一系列的方法和屬性,用來滿足用戶對(duì)路徑的處理需求。
- path.join() 方法,用來將多個(gè)路徑片段拼接成一個(gè)完整的路徑字符串
- path.basename() 方法,用來從路徑字符串中,將文件名解析出來
如果要在 JavaScript 代碼中,使用 path 模塊來處理路徑,則需要使用如下的方式先導(dǎo)入它:
const path = require('path')
路徑拼接
path.join()的語法格式
使用 path.join() 方法,可以把多個(gè)路徑片段拼接為完整的路徑字符串,語法格式如下:
path.join([...paths])
使用 path.join() 方法,可以把多個(gè)路徑片段拼接為完整的路徑字符串:
const path = require('path'); console.log( path.join('a', 'b', 'c') ); // a/b/c console.log( path.join('a', '/b/', 'c') ); // a/b/c console.log( path.join('a', '/b/', 'c', 'index.html') ); // a/b/c/index.html console.log( path.join('a', 'b', '../c', 'index.html') ); // a/c/index.html console.log(__dirname); // node自帶的全局變量,表示當(dāng)前js文件所在的絕對(duì)路徑 // 拼接成績(jī).txt的絕對(duì)路徑 console.log( path.join(__dirname, '成績(jī).txt') ); // ------ 最常用的
獲取路徑中的文件名
1.path.basename()的語法格式
使用 path.basename() 方法,可以獲取路徑中的最后一部分,經(jīng)常通過這個(gè)方法獲取路徑中的文件名,語法格式如下:
path.basename(path[,ext])
- path 必選參數(shù),表示一個(gè)路徑的字符串
- ext 可選參數(shù),表示可選的文件擴(kuò)展名
- 返回: 表示路徑中的最后一部分
2.path.basename()的代碼示例
使用 path.basename() 方法,可以從一個(gè)文件路徑中,獲取到文件的名稱部分:
// 找文件名 console.log( path.basename('index.html') ); // index.html console.log( path.basename('a/b/c/index.html') ); // index.html console.log( path.basename('a/b/c/index.html?id=3') ); // index.html?id=3 console.log(path.basename('/api/getbooks')) // getbooks
獲取路徑中的文件擴(kuò)展名
1.path.extname()的語法格式
使用 path.extname() 方法,可以獲取路徑中的擴(kuò)展名部分,語法格式如下:
path.extname(path)
- path 必選參數(shù),表示一個(gè)路徑的字符串
- 返回: 返回得到的擴(kuò)展名字符串
使用 path.extname() 方法,可以獲取路徑中的擴(kuò)展名部分
// 找字符串中,最后一個(gè)點(diǎn)及之后的字符 console.log( path.extname('index.html') ); // .html console.log( path.extname('a.b.c.d.html') ); // .html console.log( path.extname('asdfas/asdfa/a.b.c.d.html') ); // .html console.log( path.extname('adf.adsf') ); // .adsf
原文地址:https://juejin.cn/post/7088650568150810638
作者:L同學(xué)啦啦啦