我试图将纯文本文件解析为Objects
的ArrayList
。问题是它有某种嵌套结构。数据看起来像这样:使用嵌套结构解析纯文本文件
S 183166621 75783-29-8 0 -1 0 0 0 0
SS 183166621 0 DE Siloxane und Silikone, di-Me, polymers mit bor oxid (B2O3)
SS 183166621 0 EN Siloxanes and Silicones, di-Me, polymers with boron oxide (B2O3)
S 183166624 3087-36-3 221-410-8 0 -1 0 0 0 0
SS 183166624 0 DE Titan(4+)ethanolat
SS 183166624 0 EN Ethanol, titanium(4+) salt
所以有一个以“S”开头的行代表某种物质。每个“S”行后面都有多个“SS”行,其中包含不同语言的相应物质的同义词。同义词的数量不固定。有的只有2个,有的有3,4个或更多。
我的想法是创建一个Substance Object
其中包含所有可能的同义词列表。
我创建了一个BufferedReader
并尝试通过线
String line;
while((line = br.readLine()) != null) {
if (line.startsWith("S\t")) {
Substance substance = new Substance();
String[] columns = line.split("\t");
// Parse columns and store them in substance Object
}
}
解析文件行改为我想开始第二内环解析开始“SS”的所有行的同义词。当到达另一个“S”行时,内循环停止。
String line;
while((line = br.readLine()) != null) {
if (line.startsWith("S\t")) {
Substance substance = new Substance();
String[] columns = line.split("\t");
// Parse columns and store them in substance Object
while((line = br.readLine()) != null) {
if (line.startsWith("SS\t")) {
Synonym synonym = new Synonym();
// Parse columns and store them in synonym Object
// Add synonym to List of Synonyms of Substance
substance.addSynonym(synonym);
}
else {
break;
}
}
}
}
问题与此是,在离开内循环后外环执行另一readline()
所以实际的“S” - 行丢失。
难道有人指着我正确的方向吗?