Node.js檔案操作可以透過fs這個module處理

讀取檔案

var fs = require("fs");
var filename = "test.html";
var encode = "utf8";

fs.readFile(filename, encode, function(error, data){
        console.log(data);
});

寫入檔案

//Write
fs.writeFile('message.txt', 'Hello Node', function (err) {
        if (err) throw err;
        console.log('It\'s saved!');
});

//Append
fs.appendFile('message.txt', 'data to append', function (err) {
        if (err) throw err;
        console.log('The "data to append" was appended to file!');
});
Categories: Node.js