Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7073633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:57:11+00:00 2026-05-28T05:57:11+00:00

i’m trying to create a Random Access file in java. I write something in

  • 0

i’m trying to create a Random Access file in java.
I write something in a new line.

  1. How can i return the address of that line in Java?

Also, I’m a bit confused with RAFs.

For example i have a file that consists of the following entries in alphabetical manner

George 10 10 8

Mary 9 10 10

Nick 8 8 8

Nickolas 10 10 9

I would like to return the grades of Nickolas.
How can i declare that in a RAF?

Is there any method that can “read(“Nickolas”)” and return to me the line?

Thanks, in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:57:11+00:00Added an answer on May 28, 2026 at 5:57 am

    Random access files usually contain binary data rather than ascii (e.g. plain text) data. The example you are showing is ascii.

    Since the data is ascii, this means it’s not as easy to seek to various places in the file. In fact, generally the approach to get the grades for Nickolas would be to read the file line by line and parse each line into columns. Then, compare the first column for Nickolas.

    For example,

    
    BufferedReader in = new BufferedReader(new FileReader("grades.txt"));
    String line = in.readLine();
    while(null != line) {
      String [] columns = line.split(" ");
      if( columns[0].equals("Nickolas") )
        System.out.println("I found the line! " + line);
      line = in.readLine();
    }
    
    

    EDIT:

    There are a number of ways to speed this up. Here are three:

    Storing all data in a HashMap

    If you don’t have too many records, or if each record doesn’t take much space, you could read them all into RAM. You can also use a HashMap to map the name of the student to their record. For example:

    
    HashMap<String, Student> grades = new HashMap<String, Student>();
    BufferedReader in = new BufferedReader(new FileReader("grades.txt"));
    String line = in.readLine();
    while(null != line) {
      String [] columns = line.split(" ");
      grades.put( column[0],
        new Student( /* create student class instance from columns */ );
      line = in.readLine();
    }
    
    

    Now, lookups will be extremely fast.

    Using a Binary Search

    If you have too many records to fit in RAM, you can write all of the student data to a random access (binary) file. Here, you have a couple of options: you can either make each record vary in length, or you can make each record have a fixed length. Fixed length records are easier for some kinds of searching, like binary searches.

    For example, if you know each record is 100 bytes, then you know how to get to the n’th record in the binary file storing the records. Basically, read 99*n bytes. Then the next 100 bytes are the 100th record.

    Thus, if the records are sorted by student name, you can very easily use a binary search to find a specific student. This approach will still be fast, albeit not as fast as the RAM-based data structure.

    Using a HashMap as an index

    Yet another option is to combine the two approaches I mentioned above. Write the data to a binary file, and store the byte offsets of the records in a hash map. The hash map can use the student name as the key as before, but then stores a long integer offset to the record in the random access file. Thus, to look up a specific student, you find the byte offset using the hash map, and then “seek” to the record in the file and then read it. This last approach works even if the records vary in length.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.