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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:58:01+00:00 2026-06-06T05:58:01+00:00

i use LineNumberReader to read text file , when call setLineNumber and getLinenumber it

  • 0

i use LineNumberReader to read text file , when call setLineNumber and getLinenumber it print == 0 But when call readLine again ์Nit thing happen how can i fix it
Here is my code

    BufferedWriter writer = new BufferedWriter(new FileWriter("text.txt"));
    writer.write("This is a line1\n" +
            "This is a line2\n" +
            "This is a line3");
    writer.newLine();
    writer.close();
    File myFile = new File("text.txt");
    FileReader fileReader = new FileReader(myFile);
    LineNumberReader reader = new LineNumberReader(fileReader);

    // Read from the FileReader.
    String lineRead = "";
    while ((lineRead = reader.readLine()) != null) {
        System.out.println(lineRead);
    }

    // Determine the number of lines that were read.
    System.out.println("Total lines read: " +
            reader.getLineNumber());

    // Reset the number of lines read.
    reader.setLineNumber(0);
    System.out.println("Total lines read after reset: " +
            reader.getLineNumber());

    String lineRead2 = "";
    while ((lineRead2 = reader.readLine()) != null) {
        System.out.println(lineRead2);
    }
    System.out.println("End");
    // Close the LineNumberReader and FileReader.
    fileReader.close();
    reader.close();

Thank

  • 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-06T05:58:03+00:00Added an answer on June 6, 2026 at 5:58 am

    You are just setting the LineNumberReader‘s line number counter, not the position in the underlying stream. See the documentation for Java’s LineNumberReader class.

    You have already processed your stream, and thus a subsequent call to readLine() returns null.

    If you want to reread the stream, you’re going to have to call fileReader.reset(), or reader.reset(), which will result in your LineNumberReader calling the reset for your FileReader.

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

Sidebar

Related Questions

Use case: 3rd party application wants to programatically monitor a text file being generated
use this website a lot but first time posting. My program creates a number
Use case: I've just entered insert mode, and typed some text. Now I want
use Tk; my $mw=new MainWindow; $mw->geometry(1024x768); $mw->Scrolled(Text, -scrollbars => s, -width => 30,-height=>10)->pack( );
use Service layer to persist data into database. But the Unit Test does not
use Control::CLI; $cli = new Control::CLI('SSH'); $cli->connect(Host=>'10.10.10.10',Username=>'user',Password=>'pwd'); $cli->waitfor('>'); $cli->print('Show XXXXXXXXXXXXXXXXXXXX| grep Active'); @f=$cli->waitfor('>'); print
use WWW::Mechanize; my $mech = WWW::Mechanize->new; $mech->get( $url ); say $mech->text; How could I
use warnings; use Test::More; use File::Find::Rule; use Test::File::Find::Rule; my $rule = File::Find::Rule->file->name('*.pl')->not_grep(qr/^\s*use\s+strict;/m, sub {
use strict; use warnings; open(FILE1, /cygdrive/c/cpros/mola.txt); my $line = <FILE1>; print $line; close(FILE1); open(FILE1,
use strict; use warnings; open(FILE1,/cygdrive/c/cpros/karthik/molk.txt); my $line = < FILE1 > ; print $line

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.