当然这是可能的。
您需要逐行读取文件。检查每一行是否包含date1
. 如果确实如此,则提取并解析您想要的数据。
这是一个示例代码,您如何使用标准 api 实现这一点:
public class ReadFileLineByLineAnExtractSomeText {
private static final String src = "test.txt";
private static final String date1 = "March 15, 2013";
@BeforeClass
public static void genTextFile() throws IOException {
OutputStream os = new FileOutputStream(src);
os.write(("blah bla foo bar\n" +
"2013001 Jaymarion Manalo L. Service Crew March 15, 2013 8:00 12:00 13:00 17:00 10.0\r\n" +
"2013001 Jaymarion Manalo L. Service Crew March 16, 2013 8:00 12:05 13:05 17:30 10.0\r" +
"... the end").getBytes());
os.close();
}
@Test
public void testReadAndExtract() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(src));
String line = br.readLine();
int lineNumber = 0;
while(line != null) {
lineNumber++;
int i = line.indexOf(date1);
if(i != -1) {
int s = i + date1.length();
int e = line.length();
System.out.println(date1 + " found in line " + lineNumber + " at index " + i + ", extract text from " + s + " to " + e);
String extractedText = line.substring(s, e);
String[] extractedTextParts = extractedText.trim().split("\\s+");
for(String part : extractedTextParts) {
if(isTime(part)) {
System.out.println(" '" + part + "'");
}
}
}
line = br.readLine();
}
br.close();
}
private boolean isTime(String part) {
return part == null ? false : part.matches("\\d{1,2}:\\d{1,2}");
}
}
输出
March 15, 2013 found in line 2 at index 42, extract text from 56 to 94
'8:00'
'12:00'
'13:00'
'17:00'