我正在写一个解析器看起来像这样的文件:用的BufferedReader(Java)的预读
LOCUS SCU49845 5028 bp DNA PLN 21-JUN-1999
DEFINITION Saccharomyces cerevisiae TCP1-beta gene, partial cds, and Axl2p
(AXL2) and Rev7p (REV7) genes, complete cds.
ACCESSION U49845
VERSION U49845.1 GI:1293613
我想通过某些标记之前的信息(定义,版本等),但一些描述覆盖多条线路,我确实需要它。使用BufferdReader读取我的文件时,这是一个问题。 我几乎通过使用mark()和reset()来计算出它,但是当执行我的程序时,我注意到它只适用于一个标签,其他标签以某种方式被跳过。这是我到目前为止的代码:
Pattern pTag = Pattern.compile("^[A-Z]{2,}");//regex: 2 or more uppercase letters is a tag
Matcher mTagCurr = pTag.matcher(line);
if (mTagCurr.find()) {
reader.mark(1000);
String nextLine = reader.readLine();
Matcher mTagNext = pTag.matcher(nextLine);
if (mTagNext.find()){
reader.reset();
continue;
}
Pattern pWhite = Pattern.compile("^\\s{6,}");
Matcher mWhite = pWhite.matcher(nextLine);
while (mWhite.find()) {
line = line.concat(nextLine);
}
System.out.println(line);
}
这段代码应该找到标签和连接说明,涵盖多行。我在这里找到的一些答案建议使用扫描仪。这不适合我。我使用的文件可能非常大(我遇到的最大文件大于50GB),并且使用BufferedReader,我希望在系统上减少压力。