回答編集履歴

1

コード内訂正とリファレンスの追記

2019/11/26 07:19

投稿

AkitoshiManabe
AkitoshiManabe

スコア5432

test CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  let jsonText;
26
26
 
27
- if( fs.stat( filepath ).isFile() ) {
27
+ if( fs.statSync( filepath ).isFile() ) {
28
28
 
29
29
  jsonText = fs.readFileSync( filepath, "utf8" );
30
30
 
@@ -40,7 +40,7 @@
40
40
 
41
41
  let jsonText = JSON.stringify(json);
42
42
 
43
- fs.saveFileSync( filepath, jsonText, "utf8" );
43
+ fs.writeFileSync( filepath, jsonText, "utf8" );
44
44
 
45
45
  }
46
46
 
@@ -68,6 +68,16 @@
68
68
 
69
69
  ```
70
70
 
71
+ 追記)上記コードは NodeJS の以下の 同期関数で表現してみました。
72
+
73
+ * [fs.statSync()](https://nodejs.org/api/fs.html#fs_fs_statsync_path_options)
74
+
75
+ * [fs.readFileSync()](https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options)
76
+
77
+ * [fs.writeFileSync()](https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options)
78
+
79
+
80
+
71
81
 
72
82
 
73
83
  なお、パフォーマンスを求めるなら、更に考える必要があります。