日志数据,java,csv

logData, java, csv

我是新来的,我在学习 Java。我的英语不是最好的。

我需要一些帮助,如果你能帮助我,我将不胜感激。

我想在 Java 中编写一个程序,将日志数据存储在 CSV 文件中。如果我打开计算机,应用程序就会启动。

程序为当月创建一个 CSV 文件,在此之前必须检查当月是否存在。

日期和时间必须 saved.this 如果当前日期存在,还必须检查。我想存储开机和关机的日期和时间。注意:我可以多次关机,但他们存储更新日期。

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();
        }

    }

}

避免遗留日期时间类

您在 java.time 包。不要这样做。仅使用 java.time 类.

文件名

我建议使用 ISO 8601 风格的文件命名。这些格式的文本按字母顺序排序时将按时间顺序排列。

显然您只需要文件名的年份和月份。 ISO 8601 标准是 YYYY-MM。

获取当前年月需要时区。对于任何给定时刻,日期在全球范围内因地区而异。如果当前时刻接近一个月的 end/beginning,则年月可以在一个地方到下个月,同时在另一个地方到上个月。

ZoneId z = ZoneId.of( "America/Montreal" ) ;
YearMonth yearMonth = YearMonth.now( z ) ;       // Get current year-month as seen in a particular time zone.

将文件名设为文本。

String fileName = yearMonth.toString() + ".csv" ;

要生成表示日期和时间的文本,请使用 ZonedDateTime

ZonedDateTime zdt = ZonedDateTime.now( z ) ;
String output = zdt.toString() ;

从那一刻起你可以获得YearMonth

YearMonth yearMonth = YearMonth.from( zdt ) ;

文件存在

显然您只想在文件不存在时才创建它。

Java 有一些遗留的 类 用于处理文件,例如 java.io.File。 Java 有一套更新、更现代的文件 类 和 input/output 称为 “NIO” (non-blocking I/O). See Oracle Tutorial

String filePathString = "/Users/basilbourque/" + fileName ;
Path path = Paths.get( filePathString );

if ( Files.exists(path) ) {
     … do nothing
}

if ( Files.notExists(path) ) {
     … proceed with creating new file
}

CSV

创建 CSV 文件已在 Stack Overflow 上多次提及。搜索以了解更多信息。

我建议使用库来帮助完成生成或解析 CSV 或制表符分隔文件的繁琐工作。我很好地利用了Apache Commons CSV。还有其他的。

我自己已经分享了 CSV 文件 here, , 和其他人的示例演示编写。

机器bootup/shutdown

The application starts if i turn the computer on.

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.

我帮不了你。我不知道如何通过 Java 连接到 bootup/shutdown。我想您可以编写一个 shell 脚本来调用您的 Java 应用程序。然后配置一些 OS-specific 机制以在适当的时间执行 shell 脚本。

我建议探索现有的日志记录工具,Log4J 是最常用的工具之一,具体取决于您要放入此日志的内容和方式,如果您需要,您可以获取 CSV 格式的日志内容,或者这些日志如果您要进一步处理它们,可以通过程序将其转换为 CSV。