java - 日志数据,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();
}
}
}
解决方案
避免遗留的日期时间类
您将麻烦的遗留日期时间类(例如java.time包SimpleDateFormat
中Date
的现代替代品)混合在一起。不要这样做。仅使用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 脚本。
推荐阅读
- random - 从配置中获取随机物品并将其放入自定义库存(Bukkit)
- c - 使用数据结构将用户给定的值放入数组中,以便稍后打印出来
- azure - Azure App Service 上的网站在 Chrome 上存在证书问题
- python - 如何在处理中检索 2D/3D 形状的顶点坐标?
- azure-active-directory - 使用 Graph API(混合设置)访问 Exchange 2016 本地邮箱
- mysql - MySQL - 删除多个表中的记录
- python - 使用朴素贝叶斯训练模型预测“用户输入”评论
- go - 模拟在 Golang Http Hander 中调用的函数
- json - 重塑 jq 嵌套文件并制作 csv
- swift - 如何使用 swift 4 查找 sqlite 数据库中的行数?