- 同步方法 sync后綴
- 異步方法 不具有后綴 有回調(diào)
系統(tǒng)和文件常識(shí)
chmod指令:修改文件權(quán)限
chmod 777 filename 相當(dāng)于 chmod a=wxr(7=4+2+1 所有人都有讀寫運(yùn)行的權(quán)限)
權(quán)限位: mode
| - | - | - | - |
|---|---|---|---|
| 權(quán)限分配 | 文件所有者 | 文件所屬組 | 其他用戶 |
| 權(quán)限項(xiàng) | 讀 | 寫 | 執(zhí)行 |
| 字符表示 | r | w | x |
| 數(shù)字(八進(jìn)制 啥都沒是0) | 4 | 2 | 1 |
執(zhí)行下面命令查看文件或者文件夾權(quán)限(linux)
ls -al
drwxr-xr-rwx 3 wang staff 96 12 20 11:19 .
一共有十位
| 位數(shù) | 權(quán)限 |
|---|---|
| 8-6 | 文件所有者權(quán)限 |
| 5-3 | 文件所屬組權(quán)限 |
| 2-0 | 其他用戶權(quán)限 |
第九位
| 代碼 | 含義 |
|---|---|
| d | 目錄文件 |
| p | 管道文件 |
| l | 連接文件 |
| - | 表示普通文件 |
| s | .cos |
| b | ? |
Windows權(quán)限默認(rèn)是可讀可寫不可執(zhí)行的 0o666
標(biāo)識(shí)位 flag
代表對(duì)文件的操作方式
r:
r+: 可以讀取 也可以寫入
rs: 可以讀
a: append 可以追加寫入
a+: 既可以讀取 也可以追加寫入
ax:
wx+:
...
S代表同步
+代表增加一個(gè)相反操作
x代表排它方式
文件描述符 fd
nodejs中 分配一個(gè)數(shù)值描述符 ID自動(dòng)遞增的 從3開始 012被 process.stdin stdout stderr 占用了
文件基本操作
- 讀取
- 寫入
- 追加寫入
- 拷貝
- 打開
- 關(guān)閉
- read/write 對(duì)于大文件或者位置大小文件有效
const fs = require('fs');
// 同步讀取 兩個(gè)參數(shù)
const buf = fs.readFileSync("data.txt",{encoding:'utf8'});
console.log(buf);
// 異步讀取 三個(gè)參數(shù)
// 注意回調(diào)函數(shù)中參數(shù)的順序 先 error 再 data 不要寫反了
const data = fs.readFile("data.txt",{encoding:'utf8'},(err,data)=>{
console.log(data);
});
// 同步寫入
fs.writeFileSync("data2.txt","寫入的內(nèi)容");
// 異步寫入
fs.writeFile("data2.txt", "還是寫入的內(nèi)容",{encoding:'utf8'}, err=>{});
// 同步追加寫入
fs.appendFileSync("data.txt", "\nthe context added");
// 異步追加寫入
fs.appendFile("data.txt","\nthe second context added",()=>{});
// 同步拷貝寫入 第二個(gè)參數(shù)是目標(biāo)文件 如果找不到的話會(huì)自動(dòng)創(chuàng)建 找到的話會(huì)覆蓋原來的內(nèi)容
fs.copyFileSync("data.txt","datacopy.txt");
// 異步拷貝寫入
fs.copyFile("data.txt","datacopy2.txt", ()=>{});
// 模擬同步拷貝函數(shù)
function copy(source, target){
const data = fs.readFileSync(source);
fs.writeFileSync(target, data);
}
copy("data.txt", "data3.txt");
// 異步打開文件
// 回調(diào)參數(shù) fd 文件描述符
// 驗(yàn)證文件描述符遞增
fs.open("data.txt", 'r', (err, fd)=>{
console.log(fd); // 3
fs.open("data2.txt",'r', (err, fd)=>{
console.log(fd); // 4
});
});
// 異步關(guān)閉
fs.open("data.txt", "r", (err, fd)=>{
console.log('opened successfully ', fd);
fs.close(fd,err=>{
console.log('關(guān)閉成功');
});
});
// readFile()
// read() buffer
// 6 個(gè)參數(shù)
// 創(chuàng)建一塊內(nèi)存大小
const buf = Buffer.alloc(100); // byte
fs.open("data.txt", "r", (err,fd)=>{
// 一個(gè)漢子3字節(jié)
// 文件描述符 將內(nèi)容讀取到buf 向buffer寫入的初始位置0 讀取長(zhǎng)度(不能超出設(shè)置的buf的長(zhǎng)度) 讀取的初始位置
// bytesRead 實(shí)際讀取的字節(jié)數(shù) buffer讀取到的字符
fs.read(fd, buf, 0, 100, 0, (err, bytesRead, buffer)=>{
console.log(bytesRead);
console.log(buffer.toString()); // Buffer e4 bd a0 00 00 00 00 00 00 00>
});
});
// write writeFile
// 通過buffer做一個(gè)中轉(zhuǎn)站 讀取一段 寫入一段 分段寫入
// 6個(gè)參數(shù)
// 存入buffer 從buffer進(jìn)行寫入
const buf = Buffer.from('Marisol hola que tal');
fs.open("data.txt", "r+", (err, fd)=>{
// 文件描述符 從buffer讀取 讀取的初始位置 讀取長(zhǎng)度 寫入的初始位置
fs.write(fd, buf, 3, 6, 3, (err, size, buffer)=>{
fs.close(fd, err=>{
console.log('closed successfully!');
});
});
});
文件目錄基本操作
- 查看權(quán)限
- stats 對(duì)象
- 創(chuàng)建
- 讀取
- 刪除
sync同步:阻塞代碼 導(dǎo)致性能差
異步: 回調(diào)函數(shù)嵌套多 推薦 Promise async/await 進(jìn)行解決
const fs = require('fs');
// 同步查看文件目錄權(quán)限 配合 try catch 使用
try {
fs.accessSync("./");
console.log('readable and writable');
} catch(e) {
console.log('not readable or writable');
}
// 異步
fs.access('./', err=>{
if(err){
console.log(' not accessable!');
} else {
console.log(' is accessable!')
}
});
// // 同步獲取文件目錄信息 stats
let file = fs.statSync('data2.txt');
console.log(file);
// // 異步
let file2 = fs.stat("data.txt",(err,data)=>{
console.log(data);
});
// 同步創(chuàng)建目錄 必須傳入目錄的前面那一串路徑是存在的 不然會(huì)報(bào)錯(cuò)(只創(chuàng)建寫在最后的目錄名)
fs.mkdirSync("b/c/d"); // b/c必須已經(jīng)存在 本次創(chuàng)建d目錄
fs.mkdir("b/f",(err=>{}));
// 異步讀取目錄
console.log(fs.readdirSync("b"))
// 同步刪除文件目錄
fs.rmdir("b/f",()=>{});
// 同步刪除文件
fs.unlinkSync("a/data2.txt");
使用Promise配合 async/await 的例子
// 讀取doc目錄下的文件
const fs = require("fs");
const path = require("path");
function getFile(){
return new Promise((resolve, reject) => {
const filePath = path.resolve(__dirname,"./doc");
fs.readdir(filePath,(err, files)=>{
const fileArr = [];
files.forEach(fileName=>{
fileArr.push("/coc/" + fileName);
});
resolve(fileArr);
});
});
}
async function getFilePath(){
const filePath = await getFile();
return filePath;
}
function insertDB(){
getFilePath().then(res=>{
// do something to insert into DB
console.log(res);
});
}
insertDB();