亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

NodeJS 基礎(chǔ)教程

NodeJS Express.js

NodeJS 緩沖&URL

NodeJS MySql

NodeJS MongoDB

NodeJS 文件(FS)

NodeJS 其他

Node.js FS 文件追加內(nèi)容

Node.js 追加內(nèi)容到文件

要將數(shù)據(jù)追加到Node.js中的文件,請使用Node FSappendFile()函數(shù)進行異步文件操作,或使用Node的 FSappendFileSync()函數(shù)進行同步文件操作。

Node.js附加文件

在本Node.js教程中,我們將學(xué)習(xí)

  • appendFile()函數(shù)語法

  • appendFileSync()函數(shù)語法

  • appendFile():將數(shù)據(jù)異步添加到文件的示例

  • appendFileSync():將數(shù)據(jù)同步添加到文件的示例

 appendFile()的語法

fs.appendFile(filepath, data, options, callback_function);

回調(diào)函數(shù)是強制性的,在將數(shù)據(jù)追加到文件完成后會調(diào)用該函數(shù)。

 appendFileSync()的語法

fs.appendFileSync(filepath, data, options);

參數(shù)說明:

  • filepath [必需] 是一個字符串,用于指定文件路徑

  • data [必需] 是您附加到文件的內(nèi)容

  • options [可選] 以指定編碼/模式/標(biāo)志

注意:如果指定的文件不存在,則會使用提供的名稱創(chuàng)建一個新文件,并將數(shù)據(jù)附加到該文件中。

示例:Node.js 使用appendFile()異步將數(shù)據(jù)追加到文件

要將數(shù)據(jù)異步添加到Node.js中的文件中,請使用appendFile()Node FS的功能,如下所示:

// 示例Node.js程序?qū)?shù)據(jù)追加到文件
var fs = require('fs'); 
 
var data = "\nLearn Node.js with the help of well built Node.js Tutorial."; 
 
// 將數(shù)據(jù)附加到文件
fs.appendFile('sample.txt',data, 'utf8', 
    // 回調(diào)函數(shù)
    function(err) {  
        if (err) throw err; 
        // 如果沒有錯誤
        console.log("Data is appended to file successfully.") 
 });

終端輸出

arjun@arjun-VPCEH26EN:~/nodejs$ node nodejs-append-to-file-example.js
Data is appended to file successfully.

追加前文件

// 示例Node.js程序?qū)?shù)據(jù)追加到文件
var fs = require('fs'); 
 
var data = "\nLearn Node.js with the help of well built Node.js Tutorial."; 
 
// 將數(shù)據(jù)附加到文件
fs.appendFileSync('sample.txt',data, 'utf8'); 
console.log("Data is appended to file successfully.")

終端輸出

arjun@arjun-VPCEH26EN:~/nodejs$ node nodejs-append-to-file-example-2.js
Data is appended to file successfully.

追加前文件

Welcome to www.jixiangtaizi.com.cn.

追加后的文件

Welcome to www.jixiangtaizi.com.cn. 
Learn Node.js with the help of well built Node.js Tutorial.

總結(jié):

在本教程- Node.js的追加到一個文件中,我們已經(jīng)學(xué)會將數(shù)據(jù)追加到Node.js的文件,同步和異步使用appendFileSync()和appendFile()節(jié)點FS的功能分別與實例Node.js的程序。