Java 在以后运行程序时,如何保存和重新加载JTree?
我们已经为Java 在以后运行程序时,如何保存和重新加载JTree?,java,swing,jtree,Java,Swing,Jtree,我们已经为JTree编写了代码,可以在其中动态添加和删除节点 然而,我们无法拯救这棵树。每次运行程序时,我们都无法获得先前创建的树 如何保存和加载JTree JTree tree=new JTree(); .... //serialization try{ FileOutputStream file= new FileOutputStream("/home/alain/Bureau/serialisation.txt"); Objec
JTree
编写了代码,可以在其中动态添加和删除节点
然而,我们无法拯救这棵树。每次运行程序时,我们都无法获得先前创建的树
如何保存和加载JTree
JTree tree=new JTree();
....
//serialization
try{
FileOutputStream file= new FileOutputStream("/home/alain/Bureau/serialisation.txt");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(tree);
}
catch(Exception e){}
//Deserialization
JTree tree2=null;
try{
FileInputStream file= new FileInputStream("/home/alain/Bureau/serialisation.txt");
ObjectInputStream in = new ObjectInputStream(file);
tree2 = (JTree) in.readObject();
}
catch(Exception e){}
请注意,字段不可序列化,因此还应序列化树模型