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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:39:56+00:00 2026-06-17T01:39:56+00:00

Lets say I have a text file called: data.txt (contains 2000 lines) How do

  • 0

Lets say I have a text file called: data.txt (contains 2000 lines)

How do I read given specific line from: 500-1500 and then 1500-2000
and display the output of specific line?

this code will read whole files (2000 line)

public static String getContents(File aFile) {

        StringBuffer contents = new StringBuffer();

        try {

        BufferedReader input = new BufferedReader(new FileReader(aFile));
        try {
            String line = null; 

            while (( line = input.readLine()) != null){
            contents.append(line);
            contents.append(System.getProperty("line.separator"));
            }
        }
        finally {
            input.close();
        }
        }
            catch (IOException ex){
            ex.printStackTrace();
        }

        return contents.toString();
}

How do I modify above code to read specific line?

  • 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-06-17T01:39:57+00:00Added an answer on June 17, 2026 at 1:39 am

    I suggest java.io.LineNumberReader. It extends BufferedReader and
    you can use its LineNumberReader.getLineNumber(); to get the current line number

    You can also use Java 7 java.nio.file.Files.readAllLines which returns a List<String> if it suits you better

    Note:

    1) favour StringBuilder over StringBuffer, StringBuffer is just a legacy class

    2) contents.append(System.getProperty("line.separator")) does not look nice
    use contents.append(File.separator) instead

    3) Catching exception seems irrelevant, I would also suggest to change your code as

    public static String getContents(File aFile) throws IOException {
        BufferedReader rdr = new BufferedReader(new FileReader("aFile"));
        try {
            StringBuilder sb = new StringBuilder();
            // read your lines
            return sb.toString();
        } finally {
            rdr.close();
        }
    }
    

    now code looks cleaner in my view. And if you are in Java 7 use try-with-resources

        try (BufferedReader rdr = new BufferedReader(new FileReader("aFile"))) {
            StringBuilder sb = new StringBuilder();
            // read your lines
            return sb.toString();
        }
    

    so finally your code could look like

    public static String[] getContents(File aFile) throws IOException {
        try (LineNumberReader rdr = new LineNumberReader(new FileReader(aFile))) {
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            for (String line = null; (line = rdr.readLine()) != null;) {
                if (rdr.getLineNumber() >= 1500) {
                    sb2.append(line).append(File.pathSeparatorChar);
                } else if (rdr.getLineNumber() > 500) {
                    sb1.append(line).append(File.pathSeparatorChar);
                }
            }
            return new String[] { sb1.toString(), sb2.toString() };
        }
    }
    

    Note that it returns 2 strings 500-1499 and 1500-2000

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

Sidebar

Related Questions

lets say i have 5 lines within a txt file called users.txt each line
Lets say I have a modestly sized text file (~850kb, 10,000+ lines) And I
Lets say I have file with sever hundred lines of text all in capital
Lets say I have a text file MyFile.txt with following content: hellosam whatsup mynameisjohn
Lets say I have a text file with the following data Username User ID
lets say I have a text file with lines as such: [4/20/11 17:07:12:875 CEST]
Lets say I have a plain text file example.txt and I have a PHP
Lets say I have a text file, and the text file contains the following:
So, lets say that I have a text file that contains some information on
Let's say I have a text file that is 100 lines long. I want

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.