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.

Results 1 to 5 of 5

Thread: What is the correct way to write to a Random Access File?

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default What is the correct way to write to a Random Access File?

    I'm using the "writeChar", "writeInt", etc methods, but they're making the file look really funky, like this:

    __john_t__smith_ 454545454__history@_______

    I have no idea what that is or why it's doing that. It looks ever weirder in the file itself, and I couldn't even copy paste it directly to here.

    This is my code:

             public void writeToFile(RandomAccessFile raf)
            {              // write info to the file
                            raf.writeUTF(fname);
                            raf.writeChar(middleI);
                            raf.writeUTF(lname);
                            raf.writeUTF(ID);
                            raf.writeUTF(major);
                            raf.writeDouble(GPA);
     
                            // close the file
                            raf.close();
             }


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: What is the correct way to write to a Random Access File?

    anyone?

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is the correct way to write to a Random Access File?

    What are you expecting it to look like? Have you reviewed the API or Oracle's tutorial for hints/suggestions?

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: What is the correct way to write to a Random Access File?

    Yes, I have. I didn't see anything that claimed to do anything other than write to the file, which is what this is kind of doing.

    I expect it to look like it looks when I write the information to a normal file, like this:

    john t smith 454545454 history

    That is, just characters that I typed in. I don't understand the weird things I'm seeing (little vertically standing rectangles standing upright between all the fields....I tried copy pasting them but it won't let me.) That doesn't look right....

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is the correct way to write to a Random Access File?

    the weird things I'm seeing (little vertically standing rectangles standing upright between all the fields.
    Read the API doc for the write methods you are using for the explanation.
    Basically java needs to save the String's length in the file so it knows how long it is. The length is written in binary.
    Those write commands do NOT write a text file.
    Use a hex editor to see what was written.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Creating a random access file
    By ineedahero in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 24th, 2013, 08:27 PM
  2. Created a Random Access File that saves gibberish to a text file
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 21st, 2012, 11:45 AM
  3. Random Access File
    By Wish.. in forum Java Theory & Questions
    Replies: 2
    Last Post: March 12th, 2012, 02:40 PM
  4. Random Access File
    By silver_unicorn in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 12th, 2009, 01:51 AM
  5. Random Access File
    By silver_unicorn in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 10th, 2009, 10:44 PM