gpt4 book ai didi

java - 无法读取日志文件并加载到 db java

转载 作者:行者123 更新时间:2023-11-30 05:52:29 28 4
gpt4 key购买 nike

我有一个包含此类数据的日志文件

2017-01-01 00:00:11.763|192.168.234.82|"GET / HTTP/1.1"|200|"swcd (unknown version) CFNetwork/808.2.16 Darwin/15.6.0"

2017-01-01 00:00:21.164|192.168.234.82|"GET / HTTP/1.1"|200|"swcd (unknown version) CFNetwork/808.2.16 Darwin/15.6.0"

我有 20 多行,我想读取每一行并首先获取一行并用管道分割 |

所以我的想法是首先创建一个简单的bean,它可以在它们的setter方法中获取单独的数据,这样我就可以将它们保存在数据库中但我无法准确阅读第一行

从我的代码中,您可以了解我想要做什么。

      public static LogBean readFile() throws IOException {

Scanner read = new Scanner(new File("/resources/access.txt"));

LogBean logBean = new LogBean();

String string = read.nextLine();
Scanner readFileByLine = new Scanner(string);

while (readFileByLine.hasNext()) {
String[] split = readFileByLine.next().split("|");
System.out.println(split[0]); // returns 2

logBean.setDateTime(LocalDateTime.parse(split[0],
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS") // java.time.format.DateTimeFormatter
));
logBean.setIp_address(split[1]);
logBean.setRequest(split[2]);
logBean.setStatus(split[3]);
logBean.setUserAgent(split[4]);
}

return logBean;

I want to use jpa here to do logBeanRepository.save(logbean)
in a continuous manner

当我运行这个时,我得到

Exception in thread "main" java.time.format.DateTimeParseException: Text '2' could not be parsed at index 0

所以我使用 system.out 进行调试,发现它只读取了整个日期中的 2 个,我做错了什么?我希望它能够连续读取并存储在数据库中

更改分割后

    String[] split = readFileByLine.next().split("\\|");
Exception in thread "main" java.time.format.DateTimeParseException: Text '2017-01-01' could not be parsed at index 10

所以现在它至少读取了一半的日期,但仍然不完整我能做什么?

最佳答案

您需要使用特定的格式化程序:

logBean.setDateTime(
LocalDateTime.parse(
split[0],
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS") // java.time.format.DateTimeFormatter
)
);

关于java - 无法读取日志文件并加载到 db java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53653431/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com