• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading a file of objects

 
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a file in which customer objects are stored using the ObjectOutputStream class. I want to read it and print each customer information on screen line by line. I know when it is a plain text file, a Scanner object could be used to go through the various records testing the presence of a next record with hasNext() method. However i am not able to do that with an ObjectInputStream reference. So far this what i have been using to read the file:

I guess it is not good practice to use a while(true) loop?
Do you people have other ways to achieve this?
Regards

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try using the available method to see if there is any remaining data.
The method is known to produce what are considered unexpected results with some stream types, but I would expect reading from a file would be okay.

An alternative to using while(true) is to have a boolean variable (say, moreDataAvailable) which is initially set to true and is set to false in the catch block and then use while(moreDataAvailable) in your loop.
The same effect as while(true)/break but probably more obvious in its intent.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I notice that the file you are reading is named myfout.txt. The file apparently contains binary Java objects, so the file should not have had the extension .txt, because it is a not a plain text file.

Apparently the file contains a number of Customer objects, and you don't know how many there are, so you keep reading in a loop until you encounter an exception. That's indeed not really the best way to do it.

I don't know what the code looks like that writes this file, but it would have been better if this code just wrote an entire ArrayList of Customer objects at once, which you could then also load in one go.

To write the file:

To read the file:

 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Joanne
Joanne, thanks for your reply. I have tried the available method as follows:

but it looks like, it always returns 0. So it does not enter the loop. Am i doing it wrongly?

@Jesper
Jesper, I also thank you for your reply. Writing all customers records in file as a List does work but i have wanted to read them one by one. The program can read the file named myfout.txt without problem. And if i open it, there are readable characters in it.


 
Marshal
Posts: 4501
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Ngom wrote:The program can read the file named myfout.txt without problem.


That's true, but it is unconventional and confusing to others to give files containing binary data a txt ending. You could also end your file name with jpg or mp3 and it would also work fine, but you don't because it is assumed that files ending with those extensions are images and compressed audio.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You could also end your file name with jpg or mp3 and it would also work fine, but you don't because it is assumed that files ending with those extensions are images and compressed audio.


I agree, Ron. If the file name is causing confusion, i am sorry for that. I had it that way. So i plead with anybody wanting to help to take it as a .bin file as Jesper pointed out.
Regards


 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to write and then read multiple individual objects from the file then all you can do (from what I can remember) is do as you do in the OP, and catch the EOF exception.
I suppose you could mark, read, and reset, but that's a lot of keruffle just to see if there's another byte there when the first thing the OIS does on readObject is a peak at the next byte itself.
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you know beforehand how many objects you're going to store you can write that number as an Integer as the first object into the file. That way you'll know upon reading how many object there are.

I would advise not to use binary serialization for long-term file storage, though (meaning, beyond the lifetime of the JVM process). Binary serialization has compatibility problems across JVMs, and across versions of the same JVM (and makes it just about impossible to use the data in non-JVM contexts). If the objects follow JavaBean conventions, you can use XML serialization as implemented by the java.beans.XMLEncoder and XMLDecoder classes.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ulf for your reply. The file was already there for me to read. I will dig on XML serialization.


 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave!

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic