wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes IOException while reading objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "IOException while reading objects" Watch "IOException while reading objects" New topic
Author

IOException while reading objects

raji toor
Greenhorn

Joined: Jun 01, 2007
Posts: 20
i don't know why this code is throwing an IOException. below is the main class
and below are helper classes
below is the class that is sending IOException while reading input objects from file im able to read the first object only and then i get Exception
[ June 20, 2007: Message edited by: raji toor ]

toor
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Well you have an infinite loop there. other then that:
Was the file opened?
Does the file contain data?

Oh might be helpful if you include the actual IOException.

G
raji toor
Greenhorn

Joined: Jun 01, 2007
Posts: 20
Well this is what i got when i passed the exception from getStaticIpDataAsList() from FileIPData class to the main method in UseSataticIPData classAnd i realy don't know what this exception means and how to get rid of it.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Silly question, but have you tried doing a google (or other) search on your actual exception (StreamCorruptedException)?

Why does StaticIpData have a main method? Might be an idea to remove it and see what happens.

Some people have reported getting this error with Java 1.6 and Tomcat. (Which i wonder if its an incompatability between tomcat using 1.5 and code using 1.6)

G
raji toor
Greenhorn

Joined: Jun 01, 2007
Posts: 20
i replaced the main method it ddn't work and then i even switched to jdk 1.5. that too didn't help. then i tried thisI initialized the streams in the constructor and that worked for single run. And on the second run i got the same Exception. whenever i clear the contents of the data.txt it works only for first run.

one more thing i would like to add if i remove boolean value in FileOutputStream constructor. my program works fine every time. but that way i will not be able to append new contents to my file and use them for later use.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
I am not 100% why, but I can tell you what your problem is.

If you pass an array of objects to the setStaticIpData and write all of them in one go (ie using a loop to iterate over the array) rather then trying to append each one to your file, as seprate calls everything works.
also your code is more efficent as you are only opening the file once.

So for some reason opening a file in apend mode cause a "marker" to me inserted after the last record written and before the next (new) record is appened, that cause the readObject method to not recognise the next "field" and thus throw the exception.

That is the cloest I can come to the reason why, and I wouldnt quote me on it, it is an explanation for the best fit of the "facts".

G

[ June 21, 2007: Message edited by: Gavin Tranter ]

[ June 21, 2007: Message edited by: Gavin Tranter ]
[ June 21, 2007: Message edited by: Gavin Tranter ]
raji toor
Greenhorn

Joined: Jun 01, 2007
Posts: 20
OK so i will now go for reading all the contents of the file before writing my single record. and write all of them again in to the file along the new record. i used to use this trick in old C in school.

but considering the situation is there any option for me like apending in IO i do not have knowledge of JDBC yet which might improve the situation i believe.

also can you please tell me why in my code declaring instance varaibles and initializing the streams in constructor worked rather than in methods worked may atleast once. I had only read somewhere that for your program try keep stream declarations and initialization to 1.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Well depending on your applications needs, it might be better to store your objects in an array of somesort, and then serialise the items of the array.

Also if you do a google search on serialazation and appending objects you might find some alternatives that suit your needs.

JDBC is for access databases.


Originally posted by raji toor:

also can you please tell me why in my code declaring instance varaibles and initializing the streams in constructor worked rather than in methods worked may atleast once.


Sorry, I dont really understand what you are asking above.
you could create the file streams something like this:


It is expensive to open streams, so where possible it is better try to only read or write to the file in one go.
raji toor
Greenhorn

Joined: Jun 01, 2007
Posts: 20
When i was using stream like this for both input and output like this i was getting streamcorruptedexception on every run and never displayed contented of file in which im storing values.

but when i switched to thisthis atleast showed me contents once on the first run when file is empty or deleted. and on the subsequent run it gave that eception. note appending has been enabled. I was asking why in previous code it never ran while in code below it runs atleast once.
raji toor
Greenhorn

Joined: Jun 01, 2007
Posts: 20
just forget the above equations. i have com to conclusion that it was all append option that playred part in exception and other scenarios it generated.

continuing with my example i changed my way of saving data to file as below. but now the problem is that the result shows only last entery in file. I am trying calling getStaticIpDataAsList from within setStaticIpData method to check wether file previousy contains some data or not, if yes copy to list and pass back else moveon. but i get EOFException for all checks on the very first iteration telling that it does not contain any further objects but not for last one and this is the data that is being displayed in the end. From this i come to the conclusion that either objects are not been written on previous iterations but only last one also the size of file does not increase on increasing the iterations. but why i can not understand ??Also checking here proves that objects are not emptySystem.out.println(si.getIpAsString());
Main class
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Hiya,
the first thing I would say is you really should consider using the following metaphor for handling any stream/reader:



This allows for the variable reference for thes tream to be used in all section of the try catch block, rather then have the calling method deal with any exception as you have it coded. Just a thought.

Given your code, the output and behaviour you are seeing is correct. This is not a problem with the code, but with the design.

See if you can figure it out, its one of those annoying little "bugs" that happen when you modifing code (well they happen to me anyway ), and fairly obivous once you have a sit and think about.

Hint 1:
Have a read of the FileOutputStream, and what happens when you tell it to create a file as you are on line 10 of FileIpData.
Then have a think about what you are telling the JVM to do and the sequence you are tell it to do them in.

Hint 2:
Try stepping though the code in an IDE (Eclipse, NetBeans, JBuilder etc).

Gavin
[ June 22, 2007: Message edited by: Gavin Tranter ]
raji toor
Greenhorn

Joined: Jun 01, 2007
Posts: 20
No itried hard but was not able to follow your hints tothe end solution. this is what i triedRegarding FileOurputStream the api constructor of only string creates a file if doesn't exist. and if it is not alooed it throws exception. in addition it also creates a FileDescriptor object which i believe tells how to access the file structure but that i think is automatic.
And regarding the program flow readObject is throwing EOFException on every. and i tried to step tjhrough in eclipse but wasn't able to. it always starts debugging of main project not current file.
[ June 22, 2007: Message edited by: raji toor ]
raji toor
Greenhorn

Joined: Jun 01, 2007
Posts: 20
can anypone please point itout whats wrong in here. i have even checked wether object is written to file or not by reading in the finally method. but don't knw why readObject in the other method thorws EOFException every time but not last one.
[ June 24, 2007: Message edited by: raji toor ]
raji toor
Greenhorn

Joined: Jun 01, 2007
Posts: 20
this is how i made it work. reading the contents of file in the same methosd where i am writing to file. but i still don't know why it didn't work as i had designed it previously separating reading and writing to file.
[ June 24, 2007: Message edited by: raji toor ]
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Basicly, when you make the call at line 10, you are overwriting the previously saved file.

So you are in effect writing a new file for the first object serialised, then the next time around the loop you are deleting it, and creating a second file.
Thus, you never read in the previous vaule for the file and that is why you only ever see the file object in the file. Like I said if you had of traced your code you would have seen that.

Using the version of youe "code" from before my hints, in FileIpData:


Then at line 18, as the first line of the if statement add:


That is how I solved the issue.

Making sure I open the file for reading of the objects before creating my new version.

G
raji toor
Greenhorn

Joined: Jun 01, 2007
Posts: 20
yes gavin you are quite right i should have called get method before creating a new file..
it did solve the issue
thankz for pointing my basic mistake.
 
 
subject: IOException while reading objects