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: How do I read data from a Random Access File?

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

    Default How do I read data from a Random Access File?

    Let's say I have a random access file that holds the names of 1000 students.

    Somewhere in there is a student with the first name "Eli". (assume only one student has the first name Eli).

    Maybe he's the 400th person in the file.

    How do I get to him???


    This code might work...but random access files don't have "hasNext()" function.

    So how am I supposed to do it?

    And if I also want Eli's last name, and I know it comes right after his first name, how would I get to that?
                            String fileName = "";
                            boolean nameFound = false;
     
                            while (raf.hasNext() && !nameFound)
                            {
                                    fileName = raf.readUTF();
     
                                    if (fileName.equals(name))
                                    {
                                            nameFound = true;
                                    }
                            }


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: How do I read data from a Random Access File?

    Quote Originally Posted by ineedahero View Post
    Let's say I have a random access file that holds the names of 1000 students.
    In general, a "random access file" simply means a file in which you can quickly "jump" back and forth at any location at any moment. Nothing more.

    If you have a file that contains N information records, and you can jump at any record using a random access, then this should mean that the content is, at least, in one of the following formats:
    1) All "records" take a well known, fixed, number of bytes (eg. 100 bytes for each record).
    2) There's some sort of header or footer (or other more complex structure) that can "index" all the records. For example, given the record numbered 34, obtain its absolute position in the file. In this scenario, records can be at fixed or variable length.

    Do you have one of these two scenarios? If not, please, clarify.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    ineedahero (December 19th, 2013)

  4. #3
    Junior Member
    Join Date
    Dec 2013
    Location
    Leander, Texas
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How do I read data from a Random Access File?

    Well, i am by no means an expert on Random Access Files, but after looking up a few things I have an idea of how you can navigate through the file.

    The RandomAccessFile has a method that returns the files length. So you could use a variable that increments as you check each byte and stops after it is equal to the length.

    Example:

    long curPos = file.getFilePointer();
     
    while(curPos < file.length()){
         String fname = file.readUTF();
         curPos = file.getFilePointer();
    }

    note: the variable file represents the RandomAccessFile and the readUTF() method reads in the next string from the file.

  5. The Following User Says Thank You to Waker3210 For This Useful Post:

    ineedahero (December 19th, 2013)

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

    Default Re: How do I read data from a Random Access File?

    Naw, I don't have any of that.

    I thought Random Access Files took care of everything automatically.

    I'm not even going to both with this stuff then. Thanks for the clarification.

  7. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: How do I read data from a Random Access File?

    Quote Originally Posted by ineedahero View Post
    Naw, I don't have any of that.

    I thought Random Access Files took care of everything automatically.
    No. Technically every file can be random accessed, if you have the appropriate API (and in Java there is java.io.RandomAccessFile). But this doesn't mean that all file types are appropriate for random access!
    For a "pure" text file it has no sense the random access, you can't say "move at line 1234", because there's no "indexing" of lines, you don't know the absolute offset of line 1234. You have to read sequentially all lines until the end of file or until you find a line that you want.

    So, if you have a "text" file, you can read on "lines" at least in two ways:
    1) java.util.Scanner, with hasNextLine() / nextLine()
    2) java.io.BufferedReader, with readLine(). Note: BufferedReader cannot be used alone for a file, you need a FileReader or a FileInputStream+InputStreamReader.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  8. The Following User Says Thank You to andbin For This Useful Post:

    ineedahero (December 19th, 2013)

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