- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 BufferedReader
将以下内容读入 TimeEntry
。
Date: 11.10.2016
start: 09:00
我知道如何将String
转换为LocalDate
,但我不知道如何在我的代码中通过BufferedReader
使用它
我想我需要这个:
Pattern p = Pattern.compile(
" * Date:*(\\\d\\\ d)\\\ .(\\\d\\\d)\\\ .(\\\d\\\d\\\d\\\d) *"
);
<小时/>
public class TimeEntry {
private LocalDate date;
private LocalTime start;
public TimeEntry(LocalDate date, LocalTime start) {
this.date = date;
this.start = start;
}
<小时/>
try {
File file = new File("MailDaten.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line = null;
String[] line_split = line.split(" ");
String test = line_split[0];
int s = test.split("@")[0].lastIndexOf(" ");
String name = test.substring(0,s);
String mail = test.substring(s+1,test.length());
while ((line = bufferedReader.readLine()) != null) {
line_split = line.split(" ");
Daten.add(
new MailEntry(
name, mail,
new TimeEntry(
line_split[3], line_split[3], line_split[4], line_split[5]
)
)
);
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
e.printStackTrace();
}
最佳答案
像这样怎么样?
String input = "Date: 11.10.2016\r\n" +
"start: 09:00\r\n";
try (BufferedReader in = new BufferedReader(new StringReader(input))) {
String dateLine = in.readLine();
String startLine = in.readLine();
if (! dateLine.matches("Date: \\d{2}\\.\\d{2}\\.\\d{4}"))
throw new IllegalArgumentException("Invalid date line: " + dateLine);
if (! startLine.matches("start: \\d{2}:\\d{2}"))
throw new IllegalArgumentException("Invalid start line: " + startLine);
LocalDate date = LocalDate.parse(dateLine.substring(6), DateTimeFormatter.ofPattern("dd.MM.uuuu"));
LocalTime start = LocalTime.parse(startLine.substring(7), DateTimeFormatter.ofPattern("HH:mm"));
LocalDateTime dateStart = LocalDateTime.of(date, start);
System.out.println(dateStart); // prints: 2016-10-11T09:00
}
关于java - 使用 bufferedreader 读取 LocalDate - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40183003/
我可以做这个科目吗?我的意思是: BufferedReader reader1 = new BufferedReader(new FileReader(new File("file"))); Bu
这个问题已经有答案了: Unhandled Exception Type IOException [duplicate] (4 个回答) Java: import statement vs fully
PrintWriter out = new PrintWriter(DoDSocket.getOutputStream(), true); BufferedReader in = ne
我是java新手。我想从 java 启动的进程中读取标准输出。我在谷歌搜索中得到了以下代码: Runtime rt = Runtime.getRuntime(); Process pr
这个问题已经有答案了: Stream closed and not reopened - Java (2 个回答) Closing Reader/Stream in Java [duplicate]
我有一个片段如下: Process proc = Runtime.getRuntime().exec(command); BufferedReader br = new BufferedReader(
1. 概述 在这个例子中,我们将使用一个BufferedReader类来读取一个名为 "sample.txt "的文件。 BufferedReader类是用来从基于字符的
我正在 youtube 上查看有关 BufferedReader 的教程 https://www.youtube.com/watch?v=yofFVbARIRU 我完全按照他的方式编写代码,但我无法让
我正在尝试编写一个方法来获取一个多行制表符分隔的文件,并将该文件的内容作为字符串数组的数组列表返回(每一行都是一个字符串[],每个这样的字符串[]都是一个元素一个数组列表)。我的问题是,我无法判断输出
我需要从 Java InputStream 中读取以零结尾的字符串。 有没有类似BufferedReader.readLine()的读取零结尾字符串的方法? 最佳答案 package com; imp
String str = ""; try { BufferedReader br = new BufferedReader(new FileRe
我用下面的样式用BufferedReader读取文件 try (BufferedReader br = new BufferedReader(new FileReader("my_file"))) {
我正在编写一些使用clojure.async的函数,以从读取/写入套接字抽象出来。我的目的是可以将值放入 channel 中以进行写入,并从要读取的 channel 中弹出。这样,用户无需担心Read
我正在尝试使用缓冲读取器读取文件,但有时它会跳过一行中的第一个字符。这是我正在阅读的文件: http://files.moonmana.com/forums/Rectangle.h 这是我得到的结果:
我是 Java 新手,我正在尝试使用此类返回 "test.csv" 的内容。这只在我使用本地文件的路径时才有效。 public class CSVtoArray2 { public stati
我正在使用 BufferedReader.readLine() 逐行读取文本文件,但突然它不读取整行,而是只读取仅第一个字符串 示例:如果文本文件中的第一行是: [98.0,20.0,-65.0]
读完整个字符串后,读者会停留在 while 中,甚至不会抛出异常。我正在通过curl 向服务器发送请求。 我尝试更改 curl 的内容类型,更改字符串的内容,并使用另一种方式读取输入,例如扫描仪,但总
我正在使用 BufferedReader 从 ma url 获取数据。 URL url = new URL("http://"); BufferedReader in = new Buffered
我有一个缓冲阅读器,由于某种原因,它不会从我从客户端发送的打印流中读取文本。这是每次 line = in.readline 时失败的点 我也检查过并且服务器已连接。 这是错误 java.net.Soc
第一次这个循环迭代效果很好,但在我按字符“y”以重复之后,下次显示时,它不会让我输入另一个名称。我不知道是什么原因造成的,但是当你需要清除输入缓冲区时,它让我想起了 C。 任何帮助我们都将不胜感激。
我是一名优秀的程序员,十分优秀!