You probably don't want to use Lists as a data structure for this. You might be better off creating a Node
type or something similar, which can contain text and child nodes, so that you can store the data in a tree / hierarchy of nodes. Something simple like this should do the trick:
public class Node {
private String text;
private List<Node> children = new ArrayList<Node>();
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public List<Node> getChildren() {
return children;
}
}
It should then be trivial to create a tree of these Node
s when you read in the file, and to use the same structure to write it back out.