• Post Reply Bookmark Topic Watch Topic
  • New Topic

readObject from Stream till EOF

 
Marco Galea
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, guys... I managed to write (export method) objects into a stream but when i try to read them back (import method) only the first one written on the list is displayed..... i tried to put them into an array and then read them one by one but my results failed. Is there maybe a way to control the method by saying continue reading until you reach end of file ??? Thanks very much and have a good day.......

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inside your while loop, you're overwriting the previous file each time, so the file will only contain the last item. Open the stream before the loop, write inside the loop, and close it after the loop, in a finally block. (Or use Java 7's "with resources".)

When reading, you have to either know exactly what you'll be reading, or handle the IOException when you try to read past the end of the file. The first approach is preferable, so you either write a single known object graph (or a small, known number), or you need to write a header telling you what the rest of the file will be.
 
Marco Galea
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:Inside your while loop, you're overwriting the previous file each time, so the file will only contain the last item. Open the stream before the loop, write inside the loop, and close it after the loop, in a finally block. (Or use Java 7's "with resources".)


Hi Jeff, first of all thanks, secondly do you mean the loop in the Import() ??

and another question please... what do you actually mean by handle the IOException. ??

thanks again and good day
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Galea wrote:
Jeff Verdegan wrote:Inside your while loop, you're overwriting the previous file each time, so the file will only contain the last item. Open the stream before the loop, write inside the loop, and close it after the loop, in a finally block. (Or use Java 7's "with resources".)


Hi Jeff, first of all thanks, secondly do you mean the loop in the Import() ??


No, I'm talking about in the exporting method. That's the only place where you're writing anything, yes? Right now, you open a new file, write an object, close the file, then go back and open that file and overwrite from the beginning, each object replacing the last. You need to open it once, write all your objects, and then close it.

and another question please... what do you actually mean by handle the IOException. ??


It's too broad a subject to cover here. Focus on one thing at a time, and when you're ready to learn about exception handling, google for something like java exception tutorial. The basic idea, however, is that when something goes wrong, you either have to fix it (which means your catch block has to do something more than just print it out), or you have to let the caller know what went wrong (which means that you either don't catch it in the first place, so the caller sees the exception directly, or you rethrow it, either as-is, or wrapped in a more layer-appropriate exception).
 
Marco Galea
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a million.... will try and let you know the outcome.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good luck, and welcome to the Ranch!
 
Marco Galea
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another thing please !!! if i did ,true after the filename and leaving the loop as it is, wouldn't i be solving the problem you mentioned about overriding each time ??

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Galea wrote:another thing please !!! if i did ,true after the filename and leaving the loop as it is, wouldn't i be solving the problem you mentioned about overriding each time ??



No, that wouldn't work. The output from writing 3 objects to 3 different ObjectOutputStreams will not be the same as writing them to 1 OOS. The OOS adds some bookkeeping information, so when you try to read it in with a single ObjectInputStream, it will appear corrupt. You'd have to create a new OIS for each OOS you created, and that's difficult to do, maybe impossible.

 
Marco Galea
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it cheers
 
Marco Galea
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
managed to have a successfull Export/Import by putting everything into an array and thanks to your help.....
Regarding the exceptions, is it possible to catch 2 exceptions within a single catch ??? cause at the moment i have to do 2 seperate catches.

for example :

 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is from Java 7 on:
If you're using Java 6 or an even older version you have no choice but to use two distinct catch statements.

However, you shouldn't need to catch NullPointerException at all. You should instead prevent it by adding the necessary checks.
 
Marco Galea
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:It is from Java 7 on:
If you're using Java 6 or an even older version you have no choice but to use two distinct catch statements.

However, you shouldn't need to catch NullPointerException at all. You should instead prevent it by adding the necessary checks.


Thanks for your reply !!! i suppose i can use also the & operator instead of | ?? as for the null exception... they were the first two exceptions that came to my mind but alas its good to know that its not good practice to catch them.
Regards
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Galea wrote:i suppose i can use also the & operator instead of | ??


Step 1: Think about what the | means there. Now think about what and & might mean there, and if it would make sense to allow it. Try to predict what will happen if you use it.

Step 2: Try it, and see if your thinking was correct.

Setp 3: If the result is not what you expected, go back and re-think it, and see if you can understand where you went wrong.

as for the null exception... they were the first two exceptions that came to my mind but alas its good to know that its not good practice to catch them.


It's generally not good practice to catch any unchecked exception (RuntimeException, Error, and their descendants).
 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I make an exception for NumberFormatException though.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!