我正在尝试逐行拆分txt文件,并希望添加标签。第一级是1个标签,第二级是2个缩进的标签,依此类推。我知道如果用户逐行添加输入但我想分割txt文件内容,该怎么做。
这是输入文件:
<company><name>xyz</name><name>ABC PQR</name><address>
<line1>G M Road</line1><line2>akurdi</line2><state>Maharashtra</state>
<city>Pune</city></address><company>
我想要这样的输出:
<company>
<name>xyz</name>
<name>ABC PQR</name>
<address>
<line1>G M Road</line1>
<line2>akurdi</line2>
<state>Maharashtra</state>
<city>Pune</city>
</address>
<company>
这是我尝试过的方法,但是不起作用:
class lineSplit {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
List<String> list = new ArrayList<String>();
String line = null;
String[] values;
while ((line = br.readLine()) != null) {
values = line.split(">");
for (String str : values) {
list.add(str + ">");
}
}
Iterator itr = list.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
br.close();
}
}
我该怎么做?