Hello,
I am having problems using an ObjectInputStream to read from a file. My program uses a File to check whether or not a specific file exists. If it exists my program will attempt to create a FileInputStream for the file and associate an ObjectInputStream with it. It should first read an int, then start reading objects. For some reason it returns an I/O error whenever I attempt to run it.

The file itself is created by using a FileOutputStream and associating an ObjectOutputStream with it. I then write an int and my objects follow that. Any ideas what my problem could be?

File checkFile = new File("test.tst");
	if(checkFile.exists())
	{
		ObjectInputStream readFile = new ObjetInputStream(new FileInputStream("test.tst");
		int bushels = readFile.readInt();
		BlueBerry firstObject = (BlueBerry) readFile.readObject();
		BlueBerry secondObject = (BlueBerry) readFile.readObject();
		//....
	}

The test.tst file is written in such a way that it matches this reading format. If anyone can see an error in how this works I would appreciate it or any sample code I could reference! Thanks!

Recommended Answers

All 2 Replies

BlueBerry class must implements java.io.Serializable interface.

It already does but thanks! I'm assuming that integers can be mixed in the same file as other objects. Is there another way to save that I could try?

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.