• 0

[JAVA]random access file question


Question

ok here's my problem....when i write out the data for a contributor, it must be all the same size so that i can seek to that position when i want to read in the data for a specific contributor....

here's the code that writes to the random access file, padding each String with spaces so that all the records are the same length...

   public void writeRaf() throws IOException{
   	 String spaces = "                    ";
   	 raf.writeUTF(last+spaces.substring(0, contributor.MAX_LASTNAME_SIZE - last.length()));
   	 raf.writeUTF(first+spaces.substring(0, contributor.MAX_FIRSTNAME_SIZE - first.length()));
   	 raf.writeUTF(town+spaces.substring(0, contributor.MAX_TOWN_SIZE - town.length()));
   	 raf.writeUTF(state+spaces.substring(0, contributor.MAX_STATE_SIZE - state.length()));
   	 raf.writeUTF(candidate+spaces.substring(0, contributor.MAX_CONTRIB_SIZE - candidate.length()));
   	 raf.writeUTF(amount+spaces.substring(0, contributor.MAX_AMOUNT_SIZE - amount.length()));
    } // end method writeRaf()
} // end class contributorList

now when i go back to read in the same Strings, i get all the spaces....how can i take the spaces off so my output is like: Jeb Bush, Miami, Florida, K $3400

i just need to cut the spaces off after i use readUTF()....any ideas???

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.