1. 程式人生 > >003-nodejs檔案寫入系統

003-nodejs檔案寫入系統

 

我們來試試nodejs的檔案寫入系統

 


---實現檔案寫入操作---

//1.載入檔案作業系統,fs模組
var fs = require('fs');

//2.實現檔案寫入操作
var msg = 'Hello World';

//3.呼叫fs.writeFile()進行檔案寫入
fs.writeFile('./hello.txt', msg , 'utf8', function(err){
    if (err){
        console.log('寫檔案出錯啦'+err);
    }else{
        console.log('ok');
    }
});

 

開啟cmd

執行node hello.js

 

發現目錄下建立了hello.txt並且已經寫入內容