Ofcource這成爲可能。
您需要逐行讀取文件。檢查每行是否包含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'
來源
2013-03-16 12:22:28
A4L
,所有你能做的就是讀取文件順序,一行一行,直到你找到合適的人。 – 2013-03-16 11:49:27
你可以遍歷所有行,檢查if(line.contains(「2013年3月15日」){'並打印它或做任何你想做的事情。 – Pshemo 2013-03-16 11:50:17