Entoverse

Reactのこと

Node.js テキストファイルに文字列を追記

f:id:mojeld:20191125134831g:plain

テキストファイルに文字列を書く

fsモジュールを使うので、先にfsrequire記述します。

const fs = require("fs");

テキストファイルを上書きします。

非同期の場合

fs.writeFile('out.txt', "文字列書き込み(writeFile)\n", (err, data) => {
    if(err) console.log(err)
    else console.log("writeFile ok")
})

同期の場合

fs.writeFileSync("out.txt", "文字列書き込み(writeFileSync)\n")

テキストファイルに文字列を追記

非同期

fs.appendFile('out.txt', "文字列追記(appendFile)\n", (err, data) => {
    if(err) console.log(err);
    else console.log('appendFile ok');
})

同期

fs.appendFileSync('out.txt', "文字列追記(appendFileSync)\n")

https://amzn.to/2JKllev