• 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

How do I change the information in a binary file streams ?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good evening,

I am creating an application you need to create and modify files binary streams.
I found no problems to implement the creation of them. As a simple example, I created a method that stores values ​​ of type double to a file. But I found problems when I need to include other value at any position of the file or even change some value.
Below is an example of the code used to create the file:



Someone could tell me how do I change any of these values ​​and how to include some other value at any position in this file?

Thanks.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not exactly sure what problem you're having, but I see a couple of issues. First you wrap up your output stream in a DataOutputStream, but then don't use it. You could write those doubles directly to "data". That's what the DataOuputStream is for. Also, when you wrap streams, you just need to close the outer one. The close() called is chained all the way down anyway.

The ByteBuffer.array() method will return the full byte array of the buffer, eight bytes in your case. That may be the root of your problem. If you try storing something smaller in there, an int for example, you'll still get back eight bytes, four of which will be garbage. I think your technique will work for doubles and longs, but fail for every other primitive type.
 
Marco Paradiso
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greg,

I could not really understand how it can revolve. I do not know if it was clear understanding of the problem.
Do you have any examples for the solution given?
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Examples of writing to a DataOutputStream:



What do you mean "how it can revolve"? Could you be clearer about what problem you are trying to solve?

 
Marco Paradiso
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greg,

My problem is I need to add new values ​​in any position or modify any value.
For example, I have a file with the following stored values​​: 12.56, 29.95 and 19.06, and must change the file by adding the value 55.30 as the first record. The file will hold the following values​ ​: 55.30, 12:56, 29.95 and 19.06. Now I want to change the value of 29.95 to 37.44. The file would be with the values ​​55.30, 12:56, 37.44 and 19.06.
My difficulty is doing this. I do not know how to solve this problem. I hope there is some way to fix this.
Thanks
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, well one way would just be to read in your whole file, modify the values as needed, then write it back out. I'll assume though that your assignment requires you to modify the file in place. The class you need for that is java.io.RandomAccessFile. The good news is that class also has writeDouble(), writeInt(), writeFloat() methods, as well as similar read methods. Where you'll have to practice a bit is using the seek() method to find the right spot in your file to make the change.

Also, you should understand that none of the writes will change the length of your file. If you try to write an int (4 bytes) into the spot where there was a long (8 bytes), you'll end up with the four bytes of the int followed by the low order four bytes of the long that was there before.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic