Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Reading from a binary file containing a list of objects

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding the method:     public static List<Item> loadItems(String filename) throws IOException, I assume the filename contains a list of objects of the type Item. In order to open the file it needs to be casted:



But if I cast the list of Item as a single item does that work? Or would I need to cast as a List of Items and if that is the case how to cast it to a List of Items (List<Item>)? Or would there be a pointer so that everytime that I do “Item it = (Item) obj.readObject();” it would read a different object. Following the reading of the file I would need to iterate through the list in order load the data in each object and convert its data to a string.
 
Marshal
Posts: 6869
182
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method readObject() returns an Object, not a list of objects, so if I understand you correctly, you will need a loop to read an object and add it to the List until the end of file is reached.
 
Rancher
Posts: 4493
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends.
Most (all?) Collections are serializable, so if it was a List<Item> that was serialized then that is what will be read back by readObject.
 
Lazarus Wald
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is how I wrote the file:



I don't know much about the Seriliazable interface but I have seen any reference to seriliazable in the signature of the classes.
 
Marshal
Posts: 68114
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That isn't the correct way to close an input stream. Use try with resources instead.
How do you know that you are serialising multiple Objects to the same file? How do you know you aren't simply overwriting the whole file and deleting previously serialised Objects?
As Dave said earlier, why not simply serialise the List?
 
Campbell Ritchie
Marshal
Posts: 68114
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lazarus Wald wrote:. . . I have seen any reference to seriliazable in the signature of the classes.

Is there a “not” missing from that sentence?
Open the object file with a text editor and see what you can find in it. Does it say anything about exceptions?
 
Lazarus Wald
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Lazarus Wald wrote:. . . I have seen any reference to seriliazable in the signature of the classes.

Is there a “not” missing from that sentence?
Open the object file with a text editor and see what you can find in it. Does it say anything about exceptions?



Yeah, you are correct, it should read: "I don't know much about the Seriliazable interface but I have not seen any reference to seriliazable in the signature of the classes."

Most of the time when there is I/O operations there is the exception in the signature of a method which I believe is to be expected.

I don't fully understand the Serializable interface (I know what interfaces are in Java), but it seems to be implemented when ObjectOutputStream and ObjectInputStream are used.

 
Marshal
Posts: 25215
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That isn't the correct way to close an input stream. Use try with resources instead.



Indeed. Doing that will close the output stream even when the loop is terminated abnormally because of a programming error. As in this example, where the loop will run off the end of the list and an exception will be thrown.
 
Lazarus Wald
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came up with the following:



I can see that there is no error during compilation but I don't have a way to test or debug it (normally I do that by inserting println in different parts of the code of the driver/tester, but I canot do that in this situation).  Any insight on its workability or ways to improve would be great.
 
Campbell Ritchie
Marshal
Posts: 68114
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have some incorrect indentation; look at lines 18‑21. You can cause yourself no end of trouble with bad indentation
Have a look at the ObjectInputStream documentation, not only the method I have linked to. Also its writing counterpart. Look at this exception, which the writing method might throw. You may find that exception inside your object file, and you can find it with the text editor as I suggested earlier. At least I have found it like that fourteen years ago.
Why have you got the cont variable? How do you know it is doing anything useful? If you serialised the List to a file, will you need to try to deserialise more than one objet?

I had heard that the serialisation mechanism was at risk of withdrawal.
 
Paul Clapham
Marshal
Posts: 25215
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lazarus Wald wrote:Any insight on its workability or ways to improve would be great.



Well, first you read the whole list (and its contents) from the file. You'd think that would be enough. But then you continue by reading each entry of the list and appending that to the list. So it looks like if all went well, you'd have two copies of each entry when you got through with that.

So it would help to know what's in the file. Does it contain a serialized version of the list? Or does it contain serialized versions of each entry of the list? In your original code it was the latter, I believe.

And by the way at line 18 you aren't ever going to have obj == null. If you look at line 14 you can see that it can't be null, or you would get an NPE. But you didn't mean to write "obj != null" in line 18 really.
 
Lazarus Wald
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You have some incorrect indentation; look at lines 18‑21. You can cause yourself no end of trouble with bad indentation



Sorry about the bad indentation... The object file is the file resulting from the compilation (an intermediate stage between compiling and link in C), so I assume in Java that is the class file. In this case class counterpart of itemIO.java, or simply itemIO.class?

Looks like I mixed up some of the variable: it and obj.  "cont" is supposed to be a control variable to control the while loop, it should be false when the “it” is null or at least that is the idea.
 
Lazarus Wald
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, first you read the whole list (and its contents) from the file. You'd think that would be enough. But then you continue by reading each entry of the list and appending that to the list. So it looks like if all went well, you'd have two copies of each entry when you got through with that.

So it would help to know what's in the file. Does it contain a serialized version of the list? Or does it contain serialized versions of each entry of the list? In your original code it was the latter, I believe.

And by the way at line 18 you aren't ever going to have obj == null. If you look at line 14 you can see that it can't be null, or you would get an NPE. But you didn't mean to write "obj != null" in line 18 really.



I don't what exactly is in the binary file that store the objects. But the classes are for a library managements system, and control stock in a library and when items are checked out or checked in. Code number for items (books,audio,video) and its number of copies, etc. These pieces of data are stored in objects with their methods, etc, which then are store in a list.

Hmmm. I may be dyslexic or something. It should be “if(it != null)” I guess.
 
Paul Clapham
Marshal
Posts: 25215
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lazarus Wald wrote:I don't what exactly is in the binary file that store the objects.



Then your first task is to find that out. There's not much point in writing code to read input whose format you don't understand.

Or alternatively, you could experiment with serializing object graphs (as the documentation calls them) and then deserializing them. To learn how it works. Assuming that this binary file actually contains serialized Java objects, that is. Perhaps it uses some other binary format?
 
Campbell Ritchie
Marshal
Posts: 68114
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lazarus Wald wrote:. . . The object file is the file resulting from the compilation (an intermediate stage between compiling and link in C), so I assume in Java that is the class file. In this case class counterpart of itemIO.java, or simply itemIO.class?  . . .

Neither. You have misunderstood what I asked. I meant the file to which you serialised your List.
 
Dave Tolls
Rancher
Posts: 4493
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lazarus Wald wrote:That is how I wrote the file:


Could you change that so it writes the List, rather than each individual entry in the List?
That way you can just read the one object in from the file as a List<Item>.
 
Lazarus Wald
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:
Could you change that so it writes the List, rather than each individual entry in the List?
That way you can just read the one object in from the file as a List<Item>.



In order to write the file I simply put the obj.writeObject(items.get(x)); inside a for loop to iterate through the contents of the list, so I imagine that it means that I wrote them individually. In this case dont I have to read them individually as well. Could I use the writeObject and readObject to write and read the whole list of object Items at once (without a loop to iterate through the different objects)?
 
Dave Tolls
Rancher
Posts: 4493
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I'm saying.
When writing do:

rather than the loop.

Then you can read:
 
Lazarus Wald
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:
Then you can read:



So instead of a loop, it would be much simple:

Yeah, that would render a much simpler solution. I know that the approach is cumbersome in comparison, but I put some effort to come up with this solution: wouldn't that also work out ?
 
Campbell Ritchie
Marshal
Posts: 68114
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lazarus Wald wrote:. . . that would render a much simpler solution.

Yes, it would. And probably more reliable.

I know that the approach is cumbersome in comparison, but I put some effort to come up with this solution . . .

Don't become attached to old work if you find a better way to do something.

Have you found out how many Objects you have written? Did you open the file with a text editor? Some if it will be illegible, but did you see anything about exceptions?
 
Lazarus Wald
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: Have you found out how many Objects you have written? Did you open the file with a text editor? Some if it will be illegible, but did you see anything about exceptions?


I was looking for a binary fiile throughout my relevant directories/folders but could not find any that would be generated by the program. It seems that it lacks a driver/tester in a main function. But then I found out that there is test routine based JUnit test (I don't know anything about it...) that I was able leverage by right clicking on it and select it to run, and it seems that itemIO.java pass most tests with the exception of TestLoadItems and TestSavedItem which make sense since I still have to implement them.
 
Campbell Ritchie
Marshal
Posts: 68114
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to find the name of the file you wrote the object to because it has to be passed to the serialising method.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!