-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogBatch.java
More file actions
32 lines (26 loc) · 1.23 KB
/
LogBatch.java
File metadata and controls
32 lines (26 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LogBatch {
public static void main(String[] args) {
String fileName = "batch_log.txt"; // 出力先のログファイル
// DateTimeFormatterで「yyyy-MM-dd HH:mm:ss」形式に整形
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
try (FileWriter writer = new FileWriter(fileName, true)) { // ← true で追記モード
// バッチ開始時刻をログに書く
writer.write("==== バッチ開始 " + LocalDateTime.now().format(formatter) + " ====\n");
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
writer.write("現在の合計: " + sum + "\n");
}
// バッチ終了時刻をログに書く
writer.write("最終合計 = " + sum + "\n");
writer.write("==== バッチ終了 " + LocalDateTime.now().format(formatter) + " ====\n\n");
System.out.println("ログを追記しました → " + fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
}