I am writing to a RandomAccessFile. When I try to do raf.writeBytes("") the textfile ignores this empty string and just removes it from the file. Is there a way so that the textfile will save this empty character?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I am writing to a RandomAccessFile. When I try to do raf.writeBytes("") the textfile ignores this empty string and just removes it from the file. Is there a way so that the textfile will save this empty character?
It's kind of like trying to find your invisibility cloak after you've forgotten where you put it. How can you save something that doesn't exist to a file? What are you trying to do? Besides the obvious, because that doesn't seem possible to me.
This is a bit difficult to explain but I am writing a program that allows the user to write to a text file using randomaccessfile. The user enters name, age, and address, and each of these items is 20 bytes, so the record length is 60 bytes. When the user wants to search for a record, they input a record number, the program goes to seek(n*60), and those 60 bytes are stored into a byte array, and is then outputted. This works fine except for when the user wants the last record. I can't find a way to add extra space after the name,age,address to fill in the 60 bytes. I am getting the error java.io.EOFException: null due to this.
Good explanation. You don't want to "pad" the records with nothing. That's not padding. Pick a padding character that you'd never expect to find in the name, age, and address fields. You could check an ASCII table and pick some bizarre character, or pick something simpler like '@' or '#' or whatever you'd like. Then you'll know to remove or ignore those characters that have been added at the end(s) of fields. I don't know if there's a standard padding character. You might search on that and see if there are any recommendations.
lordofrandom (October 17th, 2013)
Oh wow thats a good idea, I'll try that out thanks.