写了一个程序,
里面的对象间是树型关系,
想保存这棵树到文件,
有什么简便的方法?
如何从文件中读回?如何保存程序现场到一个文件中?
又如何读取?多多指教!

解决方案 »

  1.   

    同意楼上如果实现了serializable,直接写入也可以
      

  2.   

    问题是我know nothing about XML,
    有没有什么文章介绍?
    短时间可否学成?
      

  3.   

    看看XmlDocument这个类,或者到网上查一下.
      

  4.   

    up 用xml。
    http://www.cnblogs.com/suny2006/archive/2006/12/16/594432.html
      

  5.   

    按照数据结构里面的方法 线索化以后 保存为LIST类型。
      

  6.   

    如果不需要保存後人为地查看,我偏向於Serializable。曾今这样保存过一个awt的ui程序,因为要求打开时恢复到记录上一次关闭时界面状态(比如一棵目录树,当前展开了那些子树)。
      

  7.   

    如果实现了serializable
    然后直接用ObjectOuputStream.writeObject(Object obj) 
    读进来的时候用Object ObjectInputStream.readObject()  
    这个东西比较有意思,特别是写树这些有很多层的组件,非常好用.下面是最简单的把一个JButton的对象写入到硬盘里,然后读取出来,再显示在JFrame上,因为JButton已经实现了Serializable,所以就不用再实现了,自己写了一下,复习一下,好久没弄过了,呵呵:
    import java.io.*;
    import javax.swing.*;public class WriteObject {
            
            public static void main(String[] args) {
                    
                    ObjectOutputStream oos = null;
                    ObjectInputStream ois = null;
                    try {
                            FileOutputStream fos = new FileOutputStream("obj.dat");
                            oos = new ObjectOutputStream(fos);                      
                            JButton button = new JButton("My Button");
                            oos.writeObject(button);
                            oos.close();
                            oos = null;
                            
                            FileInputStream fis = new FileInputStream("obj.dat");
                            ois = new ObjectInputStream(fis);
                            JButton button2 = (JButton)ois.readObject();
                            ois.close();
                            ois = null;
                            
                            JFrame frame = new JFrame("Example");
                            frame.getContentPane().add(button2);
                            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                            frame.setSize(400, 400);
                            frame.setVisible(true);
                    } catch(IOException ioex) {
                            System.out.println(ioex.getMessage());
                    } catch(ClassNotFoundException cnex) {
                            System.out.println(cnex.getMessage());
                    } finally {
                            try {
                                    if (oos != null)
                                    oos.close();
                                    if (ois != null)
                                    ois.close();
                            } catch(IOException ioex2) {
                                    
                            }
                            
                    }
            }
    }
      

  8.   

    用序列化最省事,但是输出的文件不能阅读;用xml的话方便扩展,jdom例子很多的