只要你的类实现了Serializable接口
在需要的时候就可以直接写到文件中
ObjectOutputStream oos = new ObjectOutStream(new FileOutputStream("c:\\obj.dat"));
oos.writeObject(vect);
oos.close();
以后就使用ObjectInputStream读回来

解决方案 »

  1.   

    好,谢谢,我实现了Serializable接口了,我试试
      

  2.   

    我在FOR中写入多个vect,成功了,但是我在ObjectInputStream又如何把多个vect给还原回来,ObjectInputStream一次只能读取一个呀。可不可以把相应的代码给贴出来
      

  3.   

    ObjectInputStream ois = null ;    
     try {
                ois = new  ObjectInputStream(new FileInputStream("D:\cache.txt")) ;
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use Options | File Templates.
            }        try {
               
                Vect c = (Vect )ois.readObject() ;
                System.out.println(c.getCode());
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use Options | File Templates.
            } catch (ClassNotFoundException e) {
                e.printStackTrace();  //To change body of catch statement use Options | File Templates.
            }这样可以读取出一个来,但是如何把多个对象给还原回来
      

  4.   

    ObjectInputStream ois = null ;    
     try {
                ois = new  ObjectInputStream(new FileInputStream("D:\cache.txt")) ;
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use Options | File Templates.
            }        try {
               //假定前面你已经写了5000个Vector对象进文件里
                List list = new LinkedList();
                for(int i = 0; i < 5000; i++){
                    Vect c = (Vect )ois.readObject() ;
                    list.add(c);
                    System.out.println(c.getCode());
                }
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use Options | File Templates.
            } catch (ClassNotFoundException e) {
                e.printStackTrace();  //To change body of catch statement use Options | File Templates.
            }
      

  5.   

    你不就是一个for循环啊!
    不断的readObject就可以知道最后有多少个啦,到达最后java会抛出IOException的啦