NodeJS 基礎(chǔ)教程

NodeJS Express.js

NodeJS 緩沖&URL

NodeJS MySql

NodeJS MongoDB

NodeJS 文件(FS)

NodeJS 其他

Node.JS FS 寫入文件

Node.JS寫入文件–要將內(nèi)容寫入Node.js中的文件,請使用 node fs模塊的writeFile() 函數(shù)。

Node.js writeFile()函數(shù)的語法

// 引入文件系統(tǒng)模塊
var fs = require('fs'); 
 
var data = "Hello !"
 
// 將數(shù)據(jù)寫入文件sample.html
fs.writeFile('sample.txt',data, 
    // 寫入文件后調(diào)用的回調(diào)函數(shù)
    function(err) {  
        if (err) throw err; 
        // 如果沒有錯(cuò)誤
        console.log("Data is written to file successfully.") 
 });

當(dāng)以上程序在Terminal中運(yùn)行時(shí),

arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node nodejs-write-to-file-example.js 
Data is written to file successfully.

示例2 –使用指定的編碼將內(nèi)容寫入文件

// 引入文件系統(tǒng)模塊
var fs = require('fs'); 
 
var data = "HELLO"; 
 
// 將數(shù)據(jù)寫入文件sample.html,指定編碼為ASCII
fs.writeFile('sample.txt',data, 'ascii', 
    // 寫入文件后調(diào)用的回調(diào)函數(shù)
    function(err) {  
        if (err) throw err; 
        // 如果沒有錯(cuò)誤
        console.log("Data is written to file successfully.") 
 });

當(dāng)以上程序在Terminal中運(yùn)行時(shí),

arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node nodejs-write-to-file-example-2.js  
Data is written to file successfully.

總結(jié):

在此Node.js教程-節(jié)點(diǎn)FS-寫入文件中,我們學(xué)習(xí)了借助示例將內(nèi)容寫入文件的過程。

丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清