我在java中编写了一个解析器,它解析了文本文件中的几个特性。我们的想法是得到一个与相应标题相对应的行块。
例如,如果我有这个:
CC -!- FUNCTION: Adapter protein implicated in the regulation of a large
CC spectrum of both general and specialized signaling pathways. Binds ...
我需要得到这个:
Function : Adapter protein implicated in the regulation of a large spectrum of both general and specialized signaling pathways. Binds ....
对于该类型的文本文件中的所有功能,我可以毫无问题地执行此操作。
当我有这个问题时,问题出现了:
CC -!- FUNCTION: Adapter protein implicated in the regulation of a large
CC spectrum of both general and specialized signaling pathway ...
CC -!- SUBUNIT: Homodimer. Interacts with SAMSN1 and PRKCE (By
CC similarity). Interacts with SSH1 and TORC2/CRTC2. Interacts ..
当我完成块"功能"时,我的解析器总是会跳到最后一行,然后通过" SUBUNIT"我不能再得到了:(
以下是我需要解析的文件示例:
CC -!- FUNCTION: Adapter protein implicated in the regulation of a large
CC spectrum of both general and specialized signaling pathways. Binds...
CC -!- SUBUNIT: Homodimer. Interacts with SAMSN1 and PRKCE (By
CC similarity). Interacts with SSH1 and TORC2/CRTC2. Interacts with ...
CC -!- SUBUNIT: Homodimer. Interacts with SAMSN1 and PRKCE salut(By
CC similarity). Interacts with SSH1 and TORC2/CRTC2. salutInteracts with
CC -!- INTERACTION:
CC Q76353:- (xeno); NbExp=3; IntAct=EBI-359815, EBI-6248077;
CC Q9P0K1-3:ADAM22; NbExp=2; IntAct=EBI-359815, EBI-1567267; ...
CC -!- SUBCELLULAR LOCATION: Cytoplasm. Melanosome. Note=Identified by
CC mass spectrometry in melanosome fractions from stage I to stage
CC IV. ....
这是我写的一部分。我在阅读时试图标记文件中的当前位置,但是当我这样做时,解析并不能很好地工作。我在这里错过了什么?
欢呼声任何帮助,我们将不胜感激:)
// Function
if (line.startsWith("CC -!- FUNCTION")) {
String data[] = line.split("CC -!- FUNCTION:");
function = function + data[1];
while ((line = bReader.readLine()) != null && ( (line.startsWith("CC ")) || (line.startsWith("CC -!- FUNCTION")) ) ) {
if (line.startsWith("CC ")) {
String dataOther[] = line.split("CC ");
function = function + dataOther[1];
prot.setFunction(function);
bReader.mark(size);
}
else if (line.startsWith("CC -!- FUNCTION")) {
String dataOther[] = line.split("CC -!- FUNCTION:");
function = function + "-!-"+ dataOther[1];
prot.setFunction(function);
bReader.mark(size);
}
}
bReader.reset();
}
// Subunit
if (line.startsWith("CC -!- SUBUNIT")) {
String data[] = line.split("CC -!- SUBUNIT:");
subunit = subunit + "-|-"+ data[1];
while ((line = bReader.readLine()) != null && ( (line.startsWith("CC ")) ) ) {
if (line.startsWith("CC ")) {
String dataOther[] = line.split("CC ");
subunit = subunit + dataOther[1];
prot.setSubunit(subunit);
}
}
//bReader.reset();
}
答案 0 :(得分:1)
.mark()和.reset()用于从缓冲区读取的更高级技术。我认为在你的情况下你只需要梳理一下从文件中读取数据。我在你的代码中看到你有多个bReader.readLine();这将从缓冲区读取一行并每次丢弃它,所以通常你只需要.readLine一次,然后处理它。
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("CC -!- FUNCTION")) {
String line2 = br.readLine();
//do some stuff
}
if (line.startsWith("CC -!- SUBUNIT")) {
String line2 = br.readLine();
//do some stuff
}
}
br.close();
我正确理解你了吗?