0

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.

10
  • 1
    How did you create the file? We'd need to see the code that created the file as well. You may have written only one object to the file. Commented Feb 6, 2022 at 0:38
  • Edited to add that file as well Commented Feb 6, 2022 at 0:47
  • 1
    What happens if you type "eclipse debugging tutorial" into a search engine? One of the most important skills a developer can have is knowing how to find information. Commented Feb 6, 2022 at 0:52
  • 2
    You are rewriting your file for each object, so I would expect that only the last object would be present. Commented Feb 6, 2022 at 1:01
  • 1
    try changing the loop so that the creation of the ObjectOutputStream is outside and only oos.writeObject(objects.get(i)); is inside of the loop. Commented Feb 6, 2022 at 1:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.