2017-06-16 90 views
0

如何將自定義文本添加到NodeJS中.csv文件的末尾?目前,我解析csv文件,並根據預定義的參數(分隔符,列號等)進行驗證,之後我必須在文件末尾添加自定義行。將自定義文本添加到NodeJS中CSV文件的末尾

例子:

Col1 Col2 Col3 Col4 
Row1 Row1 Row1 Row1 
Row2 Row2 Row2 Row2 
Row3 Row3 Row3 Row3 

和結束時,應該有定製符合行結束符。

例:

this is the end of file\n 

回答

2

您可以簡單地使用appendFile

Asynchronously: 

const fs = require('fs'); 

fs.appendFile('yourfile.csv', 'this is the end of file\n', function (err) { 
    if (err) throw err; 
    console.log('Saved!'); 
}); 

Synchronously: 

const fs = require('fs'); 

fs.appendFileSync('yourfile.csv', 'this is the end of file\n'); 
相關問題