node總結之檔案操作系列(二)
阿新 • • 發佈:2018-11-02
接著上一篇部落格來啊,咱們繼續看非同步模式下關閉檔案的語法格式:
fs.close(fd, callback)
引數描述如下:
-
fd - 通過 fs.open() 方法返回的檔案描述符。
-
callback - 回撥函式,沒有引數。
例項如下:
var fs = require("fs"); var buf = new Buffer.alloc(1024); console.log("準備開啟檔案!"); fs.open('input.txt', 'r+', function(err, fd) { if (err) { return console.error(err); } console.log("檔案開啟成功!"); console.log("準備讀取檔案!"); fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){ if (err){ console.log(err); } // 僅輸出讀取的位元組 if(bytes > 0){ console.log(buf.slice(0, bytes).toString()); } // 關閉檔案 fs.close(fd, function(err){ if (err){ console.log(err); } console.log("檔案關閉成功"); }); }); });
再來看非同步模式下擷取檔案的語法格式:
fs.ftruncate(fd, len, callback)
這個方法也會檔案描述符來讀取檔案,來看引數描述:
-
fd - 通過 fs.open() 方法返回的檔案描述符。
-
len - 檔案內容擷取的長度。
-
callback - 回撥函式,沒有引數。
例項如下:
var fs = require("fs"); var buf = new Buffer.alloc(1024); console.log("準備開啟檔案!"); fs.open('input.txt', 'r+', function(err, fd) { if (err) { return console.error(err); } console.log("檔案開啟成功!"); console.log("截取了10位元組後的檔案內容。"); // 擷取檔案 fs.ftruncate(fd, 10, function(err){ if (err){ console.log(err); } console.log("檔案擷取成功。"); console.log("讀取相同的檔案"); fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){ if (err){ console.log(err); } // 僅輸出讀取的位元組 if(bytes > 0){ console.log(buf.slice(0, bytes).toString()); } // 關閉檔案 fs.close(fd, function(err){ if (err){ console.log(err); } console.log("檔案關閉成功!"); }); }); }); });
來看刪除檔案的語法格式:
fs.unlink(path, callback)
引數描述如下:
-
path - 檔案路徑。
-
callback - 回撥函式,沒有引數。
例項如下:
var fs = require("fs");
console.log("準備刪除檔案!");
fs.unlink('input.txt', function(err) {
if (err) {
return console.error(err);
}
console.log("檔案刪除成功!");
});
還有就是建立目錄的語法格式:
fs.mkdir(path[, mode], callback)
引數描述如下:
-
path - 檔案路徑。
-
mode - 設定目錄許可權,預設為 0777。
-
callback - 回撥函式,沒有引數。
例項如下:
var fs = require("fs");
console.log("建立目錄 /tmp/test/");
fs.mkdir("/tmp/test/",function(err){
if (err) {
return console.error(err);
}
console.log("目錄建立成功。");
});
還有讀取目錄的語法格式:
fs.readdir(path, callback)
來看引數描述:
-
path - 檔案路徑。
-
callback - 回撥函式,回撥函式帶有兩個引數err, files,err 為錯誤資訊,files 為 目錄下的檔案陣列列表。
例項如下:
var fs = require("fs");
console.log("檢視 /tmp 目錄");
fs.readdir("/tmp/",function(err, files){
if (err) {
return console.error(err);
}
files.forEach( function (file){
console.log( file );
});
});
刪除目錄的語法格式如下:
fs.rmdir(path, callback)
引數說明如下:
-
path - 檔案路徑。
-
callback - 回撥函式,沒有引數。
例項如下:
var fs = require("fs");
// 執行前建立一個空的 /tmp/test 目錄
console.log("準備刪除目錄 /tmp/test");
fs.rmdir("/tmp/test",function(err){
if (err) {
return console.error(err);
}
console.log("讀取 /tmp 目錄");
fs.readdir("/tmp/",function(err, files){
if (err) {
return console.error(err);
}
files.forEach( function (file){
console.log( file );
});
});
});
最後來個Node.js 檔案模組相同的方法列表:
序號 | 方法 & 描述 |
---|---|
1 | fs.rename(oldPath, newPath, callback) 非同步 rename().回撥函式沒有引數,但可能丟擲異常。 |
2 | fs.ftruncate(fd, len, callback) 非同步 ftruncate().回撥函式沒有引數,但可能丟擲異常。 |
3 | fs.ftruncateSync(fd, len) 同步 ftruncate() |
4 | fs.truncate(path, len, callback) 非同步 truncate().回撥函式沒有引數,但可能丟擲異常。 |
5 | fs.truncateSync(path, len) 同步 truncate() |
6 | fs.chown(path, uid, gid, callback) 非同步 chown().回撥函式沒有引數,但可能丟擲異常。 |
7 | fs.chownSync(path, uid, gid) 同步 chown() |
8 | fs.fchown(fd, uid, gid, callback) 非同步 fchown().回撥函式沒有引數,但可能丟擲異常。 |
9 | fs.fchownSync(fd, uid, gid) 同步 fchown() |
10 | fs.lchown(path, uid, gid, callback) 非同步 lchown().回撥函式沒有引數,但可能丟擲異常。 |
11 | fs.lchownSync(path, uid, gid) 同步 lchown() |
12 | fs.chmod(path, mode, callback) 非同步 chmod().回撥函式沒有引數,但可能丟擲異常。 |
13 | fs.chmodSync(path, mode) 同步 chmod(). |
14 | fs.fchmod(fd, mode, callback) 非同步 fchmod().回撥函式沒有引數,但可能丟擲異常。 |
15 | fs.fchmodSync(fd, mode) 同步 fchmod(). |
16 | fs.lchmod(path, mode, callback) 非同步 lchmod().回撥函式沒有引數,但可能丟擲異常。Only available on Mac OS X. |
17 | fs.lchmodSync(path, mode) 同步 lchmod(). |
18 | fs.stat(path, callback) 非同步 stat(). 回撥函式有兩個引數 err, stats,stats 是 fs.Stats 物件。 |
19 | fs.lstat(path, callback) 非同步 lstat(). 回撥函式有兩個引數 err, stats,stats 是 fs.Stats 物件。 |
20 | fs.fstat(fd, callback) 非同步 fstat(). 回撥函式有兩個引數 err, stats,stats 是 fs.Stats 物件。 |
21 | fs.statSync(path) 同步 stat(). 返回 fs.Stats 的例項。 |
22 | fs.lstatSync(path) 同步 lstat(). 返回 fs.Stats 的例項。 |
23 | fs.fstatSync(fd) 同步 fstat(). 返回 fs.Stats 的例項。 |
24 | fs.link(srcpath, dstpath, callback) 非同步 link().回撥函式沒有引數,但可能丟擲異常。 |
25 | fs.linkSync(srcpath, dstpath) 同步 link(). |
26 | fs.symlink(srcpath, dstpath[, type], callback) 非同步 symlink().回撥函式沒有引數,但可能丟擲異常。 type 引數可以設定為 'dir', 'file', 或 'junction' (預設為 'file') 。 |
27 | fs.symlinkSync(srcpath, dstpath[, type]) 同步 symlink(). |
28 | fs.readlink(path, callback) 非同步 readlink(). 回撥函式有兩個引數 err, linkString。 |
29 | fs.realpath(path[, cache], callback) 非同步 realpath(). 回撥函式有兩個引數 err, resolvedPath。 |
30 | fs.realpathSync(path[, cache]) 同步 realpath()。返回絕對路徑。 |
31 | fs.unlink(path, callback) 非同步 unlink().回撥函式沒有引數,但可能丟擲異常。 |
32 | fs.unlinkSync(path) 同步 unlink(). |
33 | fs.rmdir(path, callback) 非同步 rmdir().回撥函式沒有引數,但可能丟擲異常。 |
34 | fs.rmdirSync(path) 同步 rmdir(). |
35 | fs.mkdir(path[, mode], callback) S非同步 mkdir(2).回撥函式沒有引數,但可能丟擲異常。 mode defaults to 0777. |
36 | fs.mkdirSync(path[, mode]) 同步 mkdir(). |
37 | fs.readdir(path, callback) 非同步 readdir(3). 讀取目錄的內容。 |
38 | fs.readdirSync(path) 同步 readdir().返回檔案陣列列表。 |
39 | fs.close(fd, callback) 非同步 close().回撥函式沒有引數,但可能丟擲異常。 |
40 | fs.closeSync(fd) 同步 close(). |
41 | fs.open(path, flags[, mode], callback) 非同步開啟檔案。 |
42 | fs.openSync(path, flags[, mode]) 同步 version of fs.open(). |
43 | fs.utimes(path, atime, mtime, callback) |
44 | fs.utimesSync(path, atime, mtime) 修改檔案時間戳,檔案通過指定的檔案路徑。 |
45 | fs.futimes(fd, atime, mtime, callback) |
46 | fs.futimesSync(fd, atime, mtime) 修改檔案時間戳,通過檔案描述符指定。 |
47 | fs.fsync(fd, callback) 非同步 fsync.回撥函式沒有引數,但可能丟擲異常。 |
48 | fs.fsyncSync(fd) 同步 fsync. |
49 | fs.write(fd, buffer, offset, length[, position], callback) 將緩衝區內容寫入到通過檔案描述符指定的檔案。 |
50 | fs.write(fd, data[, position[, encoding]], callback) 通過檔案描述符 fd 寫入檔案內容。 |
51 | fs.writeSync(fd, buffer, offset, length[, position]) 同步版的 fs.write()。 |
52 | fs.writeSync(fd, data[, position[, encoding]]) 同步版的 fs.write(). |
53 | fs.read(fd, buffer, offset, length, position, callback) 通過檔案描述符 fd 讀取檔案內容。 |
54 | fs.readSync(fd, buffer, offset, length, position) 同步版的 fs.read. |
55 | fs.readFile(filename[, options], callback) 非同步讀取檔案內容。 |
56 | fs.readFileSync(filename[, options]) |
57 | fs.writeFile(filename, data[, options], callback) 非同步寫入檔案內容。 |
58 | fs.writeFileSync(filename, data[, options]) 同步版的 fs.writeFile。 |
59 | fs.appendFile(filename, data[, options], callback) 非同步追加檔案內容。 |
60 | fs.appendFileSync(filename, data[, options]) The 同步 version of fs.appendFile. |
61 | fs.watchFile(filename[, options], listener) 檢視檔案的修改。 |
62 | fs.unwatchFile(filename[, listener]) 停止檢視 filename 的修改。 |
63 | fs.watch(filename[, options][, listener]) 檢視 filename 的修改,filename 可以是檔案或目錄。返回 fs.FSWatcher 物件。 |
64 | fs.exists(path, callback) 檢測給定的路徑是否存在。 |
65 | fs.existsSync(path) 同步版的 fs.exists. |
66 | fs.access(path[, mode], callback) 測試指定路徑使用者許可權。 |
67 | fs.accessSync(path[, mode]) 同步版的 fs.access。 |
68 | fs.createReadStream(path[, options]) 返回ReadStream 物件。 |
69 | fs.createWriteStream(path[, options]) 返回 WriteStream 物件。 |
70 | fs.symlink(srcpath, dstpath[, type], callback) 非同步 symlink().回撥函式沒有引數,但可能丟擲異常。 |
如果想了解更多,可以戳這裡。
好啦,本次記錄就到這裡了。
如果感覺不錯的話,請多多點贊支援哦。。。