aspose file tools*
The moose likes Beginning Java and the fly likes random access file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "random access file" Watch "random access file" New topic
Author

random access file

Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Hi, I have a program that writes to and reads from a random access file. That is, it's trying to...

The program loops around (it has a menu that runs in a loop until user chooses to quit), and one of the operations is to read from the file what is in there. The file contains records of an ADT I have developed, but when the method returns objects representing each of these records, instead of outputting the String, the int and the double that I know is saved in the file for each record, it outputs the name of the data type of each record, along with some garble starting with an @sign. Does this look familiar to anyone?

I want the method to return the contents of one record to the driver program , but instead what is output from the driver program (using the toString() method on the object passed as a parameter) is something along the lines of: basicRecord@130c19b.

Any idea what I may be doing wrong?

Thanks!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3561
    
  15
You need to override the toString() method in your basicRecord class. What you are seeing is the output from the Object.toString() method.


Joanne
Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Hi, I followed your advice and it now returns the data in the record. Thanks!

However, there is a new problem now, in that one of the fields is a String variable. Since I wanted fixed length records, I have a method in the basic record class that "pads out" the String so that it fills the fixed length. When the data in the Record object is returned (using the overridden toString method), it displays the empty bytes as well (they look like little squares).

I'll include the relevant methods in a simplified example:

The following method in the driver program steps through each record sequentially (I know, why sequentially? Will write a hashing algorithm for direct access next):


How can I ensure that the readRecord method only returns those bytes in the String that contain actual characters, and not the unsightly little sqares?

Thanks!
[ September 28, 2005: Message edited by: Flo Powers ]
Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
I found the answer. Once the byte array has been crammed back into a String with the required size, simply just trim the empty bytes off the end using the trim( ) method.


[ September 29, 2005: Message edited by: Flo Powers ]
[ September 29, 2005: Message edited by: Flo Powers ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: random access file