Nodejs 操作檔案流 fs 同步非同步

2021-08-27 19:22:48 字數 990 閱讀 3339

// 預設讀取檔案是非同步的

var fs = require('fs');

console.log('begin read a file');

var data = 321;

fs.readfile('./wechat-menu.json', function(err, data));

console.log('finished read a file');

console.log(data);

console.log('finished read a file');

// 結果如下

begin read a file

finished read a file

321finished read a file

console.log('begin read a file');

var data = fs.readfilesync('./wechat-menu.json');

data = json.parse(data);

console.log(data.button);

console.log('finished read a file');

// 結果如下

begin read a file

[ ] } ]

finished read a file

// 寫入檔案

fs.writefile('delete.txt','1234567890',function(err));

// 刪除檔案

fs.unlink('delete.txt', function());

// 修改檔名稱

fs.rename('delete.txt','anew.txt',function(err));

});// 判斷檔案是否存在

fs.exists('a.txt', function( exists ));

// 將資料新增到檔案末尾

檔案操作fs(03) 大檔案操作(檔案流)

大檔案操作 流式操作 記憶體開銷會比較小,速度更快 fs.createreadstream path options fs.createwritestream path options const path require path const fs require fs let spath pat...

nodejs 操作檔案

1.首先引入模組fs varfs require fs 2.方法 2.1非同步刪除檔案 fs.unlink home hello function err console.log 成功刪除檔案 2.2同步 刪除檔案 fs.unlinksync home hello console.log 成功刪除檔...

nodeJs檔案系統(fs)與流(stream)

一 簡介 本文將介紹node.js檔案系統 fs 和流 stream 的一些api已經引數使用情況。二 目錄 檔案系統將介紹以下方法 1.fs.readfile 2.fs.writefile 3.fs.open 4.fs.read 5.fs.stat 6.fs.close 7.fs.mkdir 8....