nodejs fs模組 一 檔案模組操作2

2021-08-22 18:02:20 字數 1202 閱讀 6246

可以不通過以檔案描述符操作的檔案操作函式

讀取檔案

fs.readfile(path, options, callback) 讀取檔案

path:檔名或檔案描述符

options:encoding 編碼 預設utf8  flag 預設為r 讀取方式

callback: err data 檔案內容

fs.readfilesync(path[, options]) 同步讀取檔案內容

寫入檔案

fs.writefile(file, data[, options], callback) 寫入檔案 如果存在則覆蓋

file:檔名或檔案描述符

data:內容 string或buffer

options: encoding utf8預設 mode 0o666 flag 預設為』w』

callback err

fs.writefilesync(file, data[, options]) 寫入檔案同步版

其他操作

fs.copyfile(src, dest, flags, callback) 將src複製為dest 如果已存在檔案 檔案會被覆蓋

flags 是乙個可選的整數,用於指定行為的拷貝操作。唯一支援的 flag 是 fs.constants.copyfile_excl,如果 dest 已經存在,則會導致拷貝操作失敗。

fs.copyfilesync(src, dest, flags) copyfile的同步版

fs.mkdir(path, mode, callback) 建立目錄

引數 path:路徑 mode 許可權 預設0o777

callback err

fs.mkdirsync(path, mode) 同步建立目錄

fs.readdir(path, options, callback) 讀取目錄引數

path:目錄 options: encoding 預設utf8 callback: err  files 檔名

fs.readdirsync(path, options) 同步獲取目錄

fs.rename(oldpath, newpath, callback) 檔案重新命名

fs.renamesync(oldpath, newpath) 同步檔案重新命名

fs.rmdir(path, callback) 刪除目錄

fs.rmdirsync(path) 同步刪除目錄

nodejs fs模組 檔案操作

如果檔案不存在會自動建立檔案,存在則覆蓋。var fs require fs fs.writefile fs.txt hello nodejs utf8 function err console.log write file has been success var fs require fs if...

06 檔案系統模組

fs模組 系統內建模組 主要用來操作檔案 let fs require fs 同步開啟檔案 let fd fs.opensync text 1 w console.log fd 寫入內容 fs.writefilesync fd,hello ink 退出 fs.closesync fd let fs ...

python(一) 檔案操作

my file open my file.txt w 用法 open 檔名 形式 其中形式有 w write r read.my file.write text 該語句會寫入先前定義好的 text my file.close 關閉檔案 this is my first test.this is th...