
如何快速入門VUE3.0:進入學習
path模組用於對路徑和檔案進行處理,提供了許多方法。
有一個需求是將路徑和檔名進行拼接。
const basePath = '/user/why' const filename = 'abc.txt'
那麼有人會使用字串拼接的方式來拼接。
const filePath = basePath + '/' + filename console.log(filePath);
這樣的結果雖然沒有問題,但是考慮到不同的系統,windows系統可以使用或\或/作為路徑分隔符,而Mac OS、Linux的Unix作業系統使用/作為路徑分隔符。

解決上述問題,我們可以使用path.resolve來進行路徑的拼接。
const path = require('path')
const basePath = '/user/why'
const filename = 'abc.txt'
const filePath = path.resolve(basePath, filename)
console.log(filePath); 
const path = require('path')
const filePath = '/User/haha/abc.txt'
console.log(path.dirname(filePath));
console.log(path.basename(filePath));
console.log(path.extname(filePath)); 
如果我們想要把多個路徑拼接,但不同的作業系統可能會使用不同的分隔符,我們可以使用path.join函數。
const path = require('path')
const basepath = '/User/haha'
const filename = 'abc.txt'
const filePath = path.join(basepath, filename)
console.log(filePath); 
資料夾拼接如果我們想要將某個檔案和資料夾拼接,可以使用path.resolve。
const basepath = 'User/haha' const filename = 'abc.txt'

path.resolve和path.join一樣也可以進行路徑的拼接,那它們的差別又是什麼呢?
const basepath = '../User/haha' const filename = './abc.txt' const othername = './haha.js' const filePath1 = path.join(basepath, filename, othername) console.log(filePath1); const filePath2 = path.resolve(basepath, filename, othername) console.log(filePath2);
我們可以看到它們的差異。

nodejs檔案系統的API大都提供三種操作方式:
同步操作檔:程式碼會被
阻塞,不會繼續執行非同步回呼函數操作檔:程式碼不會被阻塞,需要傳入回呼函數,當取得到結果時,回呼函數執行
非同步Promise操作檔:程式碼不會被阻塞,透過fs.promises呼叫方法操作,會傳回一個Promise,可以透過then、catch進行處理。
方式一同步操作:fs.statSync
const fs = require('fs')
const filepath = './abc.txt'
const info = fs.statSync(filepath)
console.log('後續需要執行的程式碼');
console.log(info); 
方式二非同步操作
fs.stat(filepath, (err, info) => {
if(err) {
console.log(err);
return
}
console.log(info);
console.log(info.isFile()); // 判斷是否為一個檔案console.log(info.isDirectory()); // 判斷是否為資料夾})
console.log('後續需要執行的程式碼');方式三: Promise
fs.promises.stat(filepath).then(info => {
console.log(info);
}).catch(err => {
console.log(err);
})
console.log('後續需要執行的程式碼');node為所有開啟的檔案指派了一個數字型的檔案描述子。所有檔案系統操作都使用這些文件描述符來識別和追蹤每個特定的文件。
fs.open()方法用來指派新的檔案描述子fd。一旦被分配,則文件描述符可用於從文件讀取資料、寫入資料至文件、或要求關於文件的資訊。
const fs = require('fs')
fs.open('./abc.txt', (err, fd) => {
if(err) {
console.log(err);
return
}
// 透過檔案描述子去取得檔案資訊fs.fstat(fd, (err, info) => {
console.log(info);
})
})fs.readFile(path[, options], callback):讀取檔案內容
fs.writeFile(path[, options], callback):往檔案中寫入內容
option參數:
flag: 寫入的方式
encoding:字元的編碼
檔案的寫入
fs.writeFile('./abc.txt', content, {flag: "a"}, err => {
console.log(err);
})檔案的讀取
fs.readFile('./abc.txt', (err, data) => {
console.log(data);
})如果不填寫encoding,傳回的結果Buffer(二進位)。

fs.readFile('./abc.txt', {encoding: 'utf-8'}, (err, data) => {
console.log(data);
}) 
使用fs.mkdir()或fs.mkdirSync建立一個新的資料夾。
const fs = require('fs')
// 建立資料夾const dirname = './haha'
if(!fs.existsSync(dirname)) {
fs.mkdir(dirname, (err) => {
console.log(err);
})
}fs.readdir
fs.readdir(dirname, (err, files) => {
console.log(files);
})取得資料夾中的所有文件,此時目錄如下圖所示,可以使用遞歸。

const fs = require('fs')
const path = require('path')
const dirname = './haha'
function getFiles(dirname) {
fs.readdir(dirname, {withFileTypes: true}, (err, files) => {
// console.log(files);
for(let file of files) {
// 判斷是否為資料夾if(file.isDirectory()) {
const filepath = path.resolve(dirname, file.name)
getFiles(filepath)
} else {
console.log(file.name);
}
}
})
}
getFiles(dirname)可以使用fs.rename對資料夾進行重新命名。
fs.rename('./haha', './xixi', err => {
console.log(err);
})