日志数据、java、csv
我是新来的,我学习Java。我的英语不是最好的 我需要一些帮助,如果你能帮助我,我将不胜感激 我想用Java编写一个程序,将日志数据存储在CSV文件中。如果我打开电脑,应用程序就会启动 程序将为当月创建CSV文件,在此之前,必须检查当月是否存在CSV文件 必须保存日期和时间。如果当前日期存在,还必须检查此项。我想存储通电和断电的日期和时间。注意:我可以关闭很多次,但它们存储更新日期日志数据、java、csv,java,csv,logging,autostart,Java,Csv,Logging,Autostart,我是新来的,我学习Java。我的英语不是最好的 我需要一些帮助,如果你能帮助我,我将不胜感激 我想用Java编写一个程序,将日志数据存储在CSV文件中。如果我打开电脑,应用程序就会启动 程序将为当月创建CSV文件,在此之前,必须检查当月是否存在CSV文件 必须保存日期和时间。如果当前日期存在,还必须检查此项。我想存储通电和断电的日期和时间。注意:我可以关闭很多次,但它们存储更新日期 import java.io.File; import java.io.FileNotFoundException
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软件包中,您将麻烦的遗留日期时间类(如SimpleDateFormat
和date
)与它们的现代替代品混合在一起。不要这样做。仅使用java.time类
文件名
我建议对文件使用样式命名。按字母顺序排序时,这些格式的文本将按时间顺序排列
显然,文件名只需要年份和月份。这方面的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有一组更新、更现代的文件和输入/输出类,称为。看
CSV
创建CSV文件已在堆栈溢出上讨论过多次。搜索以了解更多信息
我建议使用库来帮助生成或解析CSV或制表符分隔的文件。我很好地利用了这个机会。还有其他的
我自己也分享了编写CSV文件的示例,以及其他可能的示例
机器启动/关闭
如果我打开电脑,应用程序就会启动
必须保存日期和时间。如果当前日期存在,还必须检查此项。我想存储通电和断电的日期和时间
我帮不了你。我不知道如何通过Java连接到启动/关闭。我想您可以编写一个shell脚本来调用Java应用程序。然后配置一些特定于操作系统的机制,以便在适当的时候执行shell脚本。我建议探索现有的日志工具,Log4J是最常用的工具之一,根据您希望在日志中放置的内容和方式,如果您需要,您可以将此日志内容作为CSV获取,或者,如果您正在进一步处理这些日志,则可以通过程序将其转换为CSV 欢迎来到堆栈溢出。只有你提供足够的信息,我们才能帮助你。请提供一些代码。阅读更多信息,我已经尝试了自己和谷歌
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
}