i'm new here and i learn Java. My english is not the best.
I need some help and would be grateful if you could help me.
I want to code a program in Java which store log data in a CSV-file. The application starts if i turn the computer on.
The program creates a CSV-file for the current month and before this happen must check if did exist for the current month.
The date and the time must be saved.this must be also checked if this current date exist. i want to store the date and time of Power On and OFF. Attention: i can shutdown many times , but they store update date.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.io.BufferedReader;
public class LoggerApp {
public static void main(String[] args) {
// Creates a CSV File with current Year and Month
String fileName = new SimpleDateFormat("MM-yyyy'.csv'").format(new Date());
// Proof of File exist and read the file
File f = new File(fileName);
if (f.exists() && !f.isDirectory()) {
FileReader fr = null;
try {
fr = new FileReader(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader br = new BufferedReader(fr);
} else {
String fileName1 = new SimpleDateFormat("MM-yyyy'.csv'").format(new Date());
}
FileWriter writer;
File datei = new File(fileName);
// LocalDateTime currentDateTime = LocalDateTime.now();
try {
LocalDateTime currentDateTime = LocalDateTime.now();
// System.out.println("Before formatting: " + currentDateTime);
writer = new FileWriter(datei);
// writer.write(currentDateTime.toString());
DateTimeFormatter changeDateTimeFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
writer.write(System.getProperty("line.separator"));
String formattedDate = changeDateTimeFormat.format(currentDateTime);
writer.write(formattedDate);
writer.flush();
writer.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}