如何从文件中连续读取两个对象?

时间:2015-04-30 12:00:30

标签: java file

我遇到了问题,我不知道如何从Java中的单个文件中读取两个对象。

这是我的代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package prova.file;

/**
 *
 * @author Stefano
 */
import java.io.*;
import java.util.*;
public class ProvaFile implements Serializable {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws ClassNotFoundException {
        // TODO code application logic here
        objectone a=new objectone();
        objecttwo b=new objecttwo();
        objectone aa=null;
        objecttwo bb=null;
        Scanner m=new Scanner(System.in);
        System.out.println("inserisci per scegliere");
        int c=m.nextInt();
        switch (c){
     case 1: try{

              ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data.dat"));
                oos.writeObject(a);
                oos.close();    

             }catch(Exception ex){
                System.out.println("Serialization Save Error : "+ex.getMessage());
            }
            break;
     case 2: try{
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data.dat", true));
                oos.writeObject(b);
                oos.reset();
                oos.flush();
                oos.close();
             }catch(Exception ex){
                System.out.println("Serialization save error: "+ex.getMessage());
             }
             break;
    case 3:try{
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("data.dat"));
        aa=(objectone)ois.readObject();
        bb=(objecttwo)ois.readObject();
        ois.close();
        }catch(IOException ex){
            System.out.println(ex.getMessage());
        }
        aa.getS();
        bb.getS();
        break;
    }
    }
}

我不知道我的错是在写还是在读,你能给我一点帮助吗?

3 个答案:

答案 0 :(得分:0)

我的建议是你要制作一个List个想要存储的对象。现在您有一个要存储的对象。因此,从文件中检索Object将创建List对象,您可以从中恢复原始对象。

答案 1 :(得分:0)

内部阅读

       ArrayList<WriteObject> woi=new ArrayList<>();
        woi=(ArrayList<WriteObject>)ois.readObject();

        for(int i=0;i<woi.size();i++){
            woi.get(i).getvalues();
        }

内写

        ArrayList<WriteObject> woi=new ArrayList<>();       
        FileOutputStream fop=new FileOutputStream("c://object.ser");
        ObjectOutputStream oos=new ObjectOutputStream(fop);
        woi.add(wo);
        woi.add(wo1);
        oos.writeObject(woi);
  
    

WriteObject是我在这里用来写对象的类wo和wo1是添加到列表中然后写入文件的对象。类似地,我在阅读和迭代内容时将对象作为arraylist返回。 getvalue方法在本地用于显示对象内容。

  

答案 2 :(得分:0)

ciao,问题是ObjectOuputStream不允许追加,因为你可以阅读here 如果你想同时序列化多个对象,你必须遵循&#34;技巧&#34;在链接中解释。

这是你修改的测试用例使其工作(我不从输入中读取,我在其他之后进行调用)。

package test;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Stefano
 */
import java.io.*;
import java.util.*;
public class ProvaFile implements Serializable {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws Exception {
        // TODO code application logic here
        objectone a=new objectone();
        objecttwo b=new objecttwo();
        objectone aa=null;
        objecttwo bb=null;



        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data.dat"));
        oos.writeObject(a);
        oos.close(); 

        ObjectOutputStream oos2 = new AppendingObjectOutputStream(new FileOutputStream("data.dat", true));
        oos2.writeObject(b);
        //oos2.reset();
        //oos2.flush();
        oos2.close();

        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("data.dat"));
        aa=(objectone)ois.readObject();
        bb=(objecttwo)ois.readObject();
        ois.close();
        aa.getS();
        bb.getS();        


    }

    public static class AppendingObjectOutputStream extends ObjectOutputStream {

          public AppendingObjectOutputStream(OutputStream out) throws IOException {
            super(out);
          }

          @Override
          protected void writeStreamHeader() throws IOException {
            // do not write a header, but reset:
            // this line added after another question
            // showed a problem with the original
            reset();
          }

        }    

    private static class objectone implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private String s="uno";

        public String getS() {
            return s;
        }

        public void setS(String s) {
            this.s = s;
        }

    }

    private static class objecttwo implements Serializable  {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private String s="due";

        public String getS() {
            return s;
        }

        public void setS(String s) {
            this.s = s;
        }

    }    

}