aspose file tools*
The moose likes I/O and Streams and the fly likes Problem with writing and reading using RandomAccessFile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Problem with writing and reading using RandomAccessFile" Watch "Problem with writing and reading using RandomAccessFile" New topic
Author

Problem with writing and reading using RandomAccessFile

Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160
Hello,

I am using RandomAccessFile to write and read data to/from a Binary File.

Writing:


Reading:


It's perfect for getting the data in a specific field position. But the problem is, the program displays some incorrect data. For example, the data in position 10 is correctly displayed as "14". The data in position 12 is supposed to display "300", however it displays "44" instead.

I tried using other methods for write and read but I'm getting other weird displays - maybe the byte positions of the actual data... this I'm not sure, I hope you can help me guys. Thank you.>


SCJP/OCPJP 6 | SCWCD/OCPJWCD 5 | OCMJEA in progress...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

If you call "write(300)", it will not actually write 300. From the API of DataOutput.write (RandomAccessFile implements DataOutput):
Writes to the output stream the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

So that 300 is turned into 44:
300 in binary: 0000 0000 0000 0000 0000 0001 0010 1100
Cut off everything but the last 8: 0010 1100
And that happens to be 44.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160
Thank you for pointing that out. Why didn't it even occur to me?

I'm trying to check the API for the "direct" methods of getting the exact 300 value. But I don't seem to get one. Does this mean that I have to convert the Binary data into Integer to display correctly?

As I've said, I tried others too, especially the writeInt() and readInt() methods but they seem to behave the same way since I'm dealing with binary data.
My data, e.g.300, is supposed to be integer value. All of it is supposed to be integer.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

An integer requires 4 bytes, and readInt and writeInt should handle that. Of course this uses Big-endian conversion, which may not be how it was originally stored.

The following to classes do exactly what I expect:
Output:
D:\>java Test

D:\>java Test2
24
Current pointer: 1
0
0
1
44
300
Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160
Rob Prime wrote:
D:\>java Test2
24
Current pointer: 1
0
0
1
44
300


Thanks so much for your time ..but I'm so sorry.. I'm really confused with the bytes and the works..

Reading the writing program, I could assume that the file, test.ser would look like this (it's in binary format but imaginatively, the record in line 1 would look like this):

24 300

I'm wondering why the output...

First output - 24. Check.
Second output for the current pointer, since the first data in pointer 0 has already been read, it is now in pointer 1. Check.

The next ones now dislodge me... because I am getting the same zeroes in my program and I couldn't figure out why.. Why do you have to read 4 times using read() then seek to the pointer then use readInt()... then get the 300 value...

I tried doing the same for the 4 bytes (just a wild guess about the reading four times), but now I get: 44, then 0s, then some 201326593 or other large-numbers. I still couldn't get the 300 output.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

One int (300) is stored in four bytes: 0, 0, 1 and 44. That was what I was trying to show you; first with the individual reads, then reseting the position and reading the 4 bytes as one int.

If you haven't written the 300 using writeInt but with write, it is written as 44 and you can never get 300 back.
Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 160
Rob Prime wrote:One int (300) is stored in four bytes: 0, 0, 1 and 44. That was what I was trying to show you; first with the individual reads, then reseting the position and reading the 4 bytes as one int.

If you haven't written the 300 using writeInt but with write, it is written as 44 and you can never get 300 back.


Oh my! It finally worked for me!
As you showed, I need to match my methods for writing and reading. Like I should use writeInt if I want to readInt... Before I use write() then readInt(), or other way around... so it all got mixed up.. Very basic.. but lesson learned. Thank you so much!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with writing and reading using RandomAccessFile