Search code examples
javaobjectinputstream

Importing multiple objects with ObjectInputStream


After implementing the suggestions I was given on my last post, I now have a working piece of code without any error messages, but my code will still only print the first object from my file. I would be grateful for any suggestions.

import java.io.*;
import java.util.*;


public class Mainclass {
    public static void main(String[] args) {
        
        InputStream is;
        try {
            is = new FileInputStream("or.rtf");
            
            ObjectInputStream ois;
            try {
                
                int l = 1;
                ois = new ObjectInputStream(is);
                
                while (l > 0) {
                    
                        
                    try {
                        
                        Stone p = (Stone) ois.readObject();
                        System.out.println(p);
                        
                        
                    }   catch(Exception e) {
                            if(e instanceof EOFException) {
                                l--;
                                System.err.println();
                            } else if(e instanceof FileNotFoundException) {
                                l--;
                                System.err.println(e);
                            } else {
                                System.err.println(e);
                            }
                    }
                }
                ois.close();
                    
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}

Code to save objects to a file:

import java.io.*;
import java.util.*;

public class Saver {
    public static void main(String[] args) {
        
        ArrayList<Stone> objects = new ArrayList<Stone>();
        objects.add(new Stone("Max",50,90));
        objects.add(new Stone("Fiona",30,60));
        objects.add(new Stone("Sam",20,30));
        
        for (int i = 0; i < objects.size(); i++ ) {
        
        
            try {
            OutputStream os = new FileOutputStream("qt.rtf");
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(objects.get(i));
            System.out.println(i);
            oos.close();
        }   catch (IOException e) {
                System.err.println(e);
            }
        }
    }
}

I implemented the println(i) to check whether the code was even executed in a loop.


Solution

  • Change the scope of ObjectOutputStream and FileOutputStream.

        public static void main(String[] args) {
            ArrayList<Stone> objects = new ArrayList<Stone>();
            objects.add(new Stone("Max",50,90));
            objects.add(new Stone("Fiona",30,60));
            objects.add(new Stone("Sam",20,30));
    
            try {
                OutputStream os = new FileOutputStream("qt.rtf");
                ObjectOutputStream oos = new ObjectOutputStream(os);
                
                for (int i = 0; i < objects.size(); i++ ) {
                    oos.writeObject(objects.get(i));
                    System.out.println(i);
                }
                oos.close();
            } catch (IOException e) {
                System.err.println(e);
            }
        }
    }
    

    You are also not reading from the same file you created or.rtf and qr.rtf.