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 7662481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:48:50+00:00 2026-05-31T13:48:50+00:00

I have a fixed format file. I want to access specific lines in this

  • 0

I have a fixed format file.

I want to access specific lines in this file based on line numbers.

e.g. read line 100

The length of each line is 200 bytes.

So directly moving cursor to 100th line using RandomAccessFile would be like:

File f = new File(myFile);
RandomAccessFile r = new RandomAccessFile(f,"rw");
r.skipBytes(200 * 99);   // linesize * (lineNum - 1)
System.out.println(r.readLine());

However, I am getting output as null.

What am I missing here ?

The question is in continuation to answer to my previous question Reaching a specific line in a file using RandomAccessFile

Update:

Below program works exactly as I am expecting:

Line size is 200 characters.

File f = new File(myFile);
RandomAccessFile r = new RandomAccessFile(f,"rw");
r.seek(201 * (lineNumber-1));   // linesize * (lineNum - 1)
System.out.println(r.readLine());

Giving linenumber (any line number from entire file) is printing that line.

@EJP: Please explain!

  • 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-31T13:48:51+00:00Added an answer on May 31, 2026 at 1:48 pm

    Firstly, if you are only going to be reading from the file, not writing to it, then I would suggest that you open the RandomAccessFile as “r” rather than “rw”. The reason is that, if the file is open to allow write access, you can actually move the pointer in the file to a location that is larger than the length of the file, because you can potentially write the file as large as you want.

    For example, if you have a file of 100 bytes in length, opening it in read-only mode will force you to keep the pointer somewhere in those 100 bytes. However, if you open the same file in read-write mode, you can tell RandomAccessFile to seek(250) and it will obey without any issues, as it thinks that you potentially want to write data out at this point in the file.

    So, as some others have stated, its possible that you have moved past the end of the file, which would be quite valid in “rw” mode.

    Secondly, if you are going to be reading from the beginning of the file, I would recommend that you use seek() rather than skipBytes(). By using seek(), you are guaranteeing that you are moving to the exact location you want to be, as it is always relative to the start of the file. However, skipBytes() is relative to the current pointer position in the file, so if you accidentally happened to move the pointer somewhere in your code between the RandomAccessFile constructor and the skipBytes() method call, you wouldn’t arrive at the correct spot.

    So, if you are going to be working from the beginning of the file, seek() provided a greater level of security that you are going to end up at the correct place in the file, no matter what else you do to the RandomAccessFile.

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

Sidebar

Related Questions

I have a fixed-length data file need to persistence into database. I use a
I am reading a text file with this format: grrr,some text,45.4321,54.22134 I just have
I have to read a binary file in a legacy format with Java. In
I have a problem with IE. I have a fixed div like this: #fixed
I want to remove digits from a float to have a fixed number of
I have my current location fixed via CoreLocation. Now I want to get those
I'm trying to parse some data in a fixed format text file where each
I have a xml file and I want to store it contents in DB.How
I have a properties (Java world) file with comments and key=value. I simply want
i have a binary file with following format : [N bytes identifier & record

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.