logData,java,csv

问题描述 投票:2回答:1

我是新来的,我学习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();
        }

    }

}
java csv logging autostart
1个回答
0
投票

避免使用旧的日期时间类

您正在将麻烦的旧式日期时间类(例如SimpleDateFormatDate)与它们的现代替代品混合在java.time包中。不要这样仅使用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" ;

文件存在

显然,您只想创建一个尚不存在的文件。

Java具有一些用于处理文件的旧类,例如java.io.File。 Java具有一组新的,更现代的文件和输入/输出类,称为“NIO” (non-blocking I/O)。参见Oracle Tutorial

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

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

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

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

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

我无法在那帮你。我不知道通过Java钩入启动/关机。

© www.soinside.com 2019 - 2024. All rights reserved.