Here is my code to write the arraylist to .dat file, And im not sure if its working but it writes to the file and i can see all the random characters so it is writing something but i keep getting io errors when i try to use the method.

/** Writes all requests to the specified file
     * @param fname name of file storing requests
     */
    public void writeRequestsToFile(String fname)
   {
       
       try
       {
            FileOutputStream fileOut = new FileOutputStream(fname);
            ObjectOutputStream oos = new ObjectOutputStream (fileOut);
            oos.writeObject (allRequests);
            oos.close();
        } catch (FileNotFoundException e) { 
            System.out.println("File Not Found");   
        } catch (IOException e) { 
            System.out.println("IO Exception");  
        }
    }
    
    /** reads all requests from the specified file and stores 
     * @param fname name of file storing requests
     */
    public void readRequestsFromFile(String fname)
   {
        try
        {
            FileInputStream fileIn = new FileInputStream(fname);
            ObjectInputStream ois = new ObjectInputStream (fileIn);
            Object obj = ois.readObject();
            allRequests = (ArrayList) ois.readObject();
            ois.close();
        } catch (FileNotFoundException e) { 
            System.out.println("File Not Found");  
        } catch (IOException e) { 
            System.out.println("IO Exception"); 
        } catch (ClassNotFoundException e) { 
            System.out.println("Class Not Found"); 
        }
    }

The Object i am Writing is an ArrayList called allRequests

Recommended Answers

All 3 Replies

Object obj = ois.readObject();
 allRequests = (ArrayList) ois.readObject();

should be?

allRequests = (ArrayList<Type>) ois.readObject();

Por ejemplo, esto es mejor:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;


public class SerializationTest implements Serializable{

	private static final long serialVersionUID = 196385420706158250L;
	static ArrayList<String> list = new ArrayList<String>();
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		list.add(new String("WOOT"));
		list.add(new String("IT'S THE"));
		list.add(new String("Serialization Test!"));

		try{
			FileOutputStream fileOut = new FileOutputStream("stuff.txt");
			ObjectOutputStream oos = new ObjectOutputStream (fileOut);
			oos.writeObject (list);
		}catch(Exception e){
			System.out.println("Stuff went wrong! NOOOOOO!!!");
		}
		
		try{
			FileInputStream fileIn = new FileInputStream("stuff.txt");
			ObjectInputStream ois = new ObjectInputStream (fileIn);
			list = (ArrayList<String>) ois.readObject();
			ois.close();
		} catch(Exception e){
			System.out.println("Best error checking ever!");
		}
		
		for (String s: list) System.out.println(s);
		
	}

}

Hmmmm i created the The Arraylist<String> list and exported it and it worked fine then i tried again with the ArrayList<Request> allRequests and it didnt work, Same Io Exception. The Request Class implements Serializable and so does the SerializableWriter Class i am using to write the .dat file.

Well, lets see your code.

edit:

And I also think, but am not positive, that in order to successfully write out the ArrayList, every Object that the ArrayList (directly or indirectly) contains must also implement Serializable except primitive types. So if you have ArrayList<Stuff> and Stuff has a DiffStuff inside it, then I *think* both Stuff and DiffStuff must implement Serializable and have unique IDs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.