有没有办法从文本文件中提取时间值。文本文件的格式为
杰克约翰逊 周日1100 - 1500,我无法找到一种方法从文本文件中提取这两次。下面这段代码是我到目前为止的,它适用于字符串,但我找不到一种方法来获取它来提取文本文件中的时间范围
Boolean isAvail = false;
Date day = new Date();
SimpleDateFormat simpleDateformat = new SimpleDateFormat("EEEE");
String token1 = "";
Scanner inFile1 = new Scanner(new File("F:\\Example\\text.txt")).useDelimiter(",\\s*");
List<String> availability = new ArrayList<String>();
while (inFile1.hasNext())
{
token1 = inFile1.next();
availability.add(token1);
}
inFile1.close();
String[] availabilityArray = availability.toArray(new String[0]);
String searchArray = simpleDateformat.format(day);
for (String curVal : availabilityArray)
{
if (curVal.contains(searchArray))
{
System.out.println(curVal);
}
}
答案 0 :(得分:1)
如果您显示的是固定格式文件,则无需使用DateFormat,扫描仪分隔符等将其复杂化。
只是逐行处理文件,按空格分割每一行,并将每个值放在数组中自己的位置,例如:
Scanner inFile1 = new Scanner(new File("F:\\Example\\text.txt"));
while (inFile1.hasNext()) {
token1 = inFile1.nextLine();
availability.add(token1);
}
inFile1.close();
System.out.println(availability.get(0));
String[] availabilityArray = availability.toArray(new String[0]);
for (String line : availabilityArray) {
System.out.println("line: " + line);
System.out.println("----");
String[] lineTokens = line.split("\\s");
System.out.println("name: " + lineTokens[0] + " " + lineTokens[1]);
System.out.println("day: " + lineTokens[2]);
System.out.println("start: " + lineTokens[3]);
System.out.println("end: " + lineTokens[5]);
System.out.println("----");
}
BT有很多可能性,更复杂,更简单,更优雅等等......