• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Binary File  RSS feed

 
Ahmed Shalotut
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Want somebody to speak with me about binary file in Java
The teachers are good but they don't give enough
so please i wanted to know about editing in a binary file considering
i have a .bin file including Objects from class X and Array Lists of Class X
now i need to read all this in an Array List then edit them >> want a code please
and another question what if i want to change some info like change some attributes of the objects in that array then put it again in the file
considering that i used APPEND in writing the file
do i need to delete the file to add the new Edition?
 
Tim Moores
Saloon Keeper
Posts: 4019
94
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How was this binary file created? You mention objects, so was it via serialization? If so, you'd need to take the serialization code as blueprint to write the deserialization code. Serialization also doesn't allow for editing a file - you'd have to recreate it from scratch.

On the other hand you mention "appending" to a file - I don't think that works with serialization. So how did you create that file?
 
Ahmed Shalotut
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i created it just like

ObjectoutputStream OOS=new ObjectOutputstream (new FileOutputStream(new File("bin.bin"),true));

 
Ahmed Shalotut
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahmed Shalotut wrote:i created it just like

ObjectoutputStream OOS=new ObjectOutputstream (new FileOutputStream(new File("bin.bin"),true));

and sure the Class was implementing Serializable

the problem is i am not sure how it really works

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Try a few links to see how serialization works: 1 (a bit old) 2 (a bit sketchy).
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahmed Shalotut wrote:
ObjectoutputStream OOS=new ObjectOutputstream (new FileOutputStream(new File("bin.bin"),true));


Appending to a file is not a good idea for serialization. The serialization protocol has a header. So, appending it means that you now have a header in the middle of the file. It will be very difficult (unless you can reverse what you did exactly) to get the file pointer right at the header, in order to deserialize the objects back.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!