首页 > 解决方案 > 日志数据,java,csv

问题描述

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

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

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

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

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

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

    }

}

标签: javacsvloggingautostart

解决方案


避免遗留的日期时间类

您将麻烦的遗留日期时间类(例如java.timeSimpleDateFormatDate的现代替代品)混合在一起。不要这样做。仅使用java.time类。

文件名

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

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

获取当前年份月份需要一个时区。对于任何给定的时刻,日期在全球范围内因区域而异。如果当前时刻接近一个月的结束/开始,则年月可能会在下个月在一个地方,而上个月在另一个地方。

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 有一套更新、更现代的文件和输入/输出类,称为“NIO”(非阻塞 I/O)。请参阅Oracle 教程

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

Stack Overflow 上多次介绍了创建 CSV 文件。搜索以了解更多信息。

我建议使用库来帮助生成或解析 CSV 或制表符分隔的文件。我充分利用了Apache Commons CSV。还有其他的。

我自己在这里这里这里以及可能的其他地方分享了演示 CSV 文件编写的示例。

机器启动/关闭

如果我打开计算机,应用程序就会启动。

必须保存日期和时间。如果当前日期存在,还必须检查此项。我想存储开机和关机的日期和时间。

我不能帮你。我不知道如何通过 Java 连接到启动/关闭。我想你可以编写一个 shell 脚本来调用你的 Java 应用程序。然后配置一些特定于操作系统的机制以在适当的时间执行 shell 脚本。


推荐阅读