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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:14:25+00:00 2026-06-13T16:14:25+00:00

I am trying to read from a text file and use the input to

  • 0

I am trying to read from a text file and use the input to create multiple different objects. I obviously don’t want to keep reading after the file is empty, so I have a method isEmpty() which reads up until the point I want in the file and then reads the next line and tests for null.

The problem is that if it’s not empty it then goes onto read the file…but from the NEXT line. In other words, I read the first line to test but then can’t read it again.

My current idea is to create a dummy object to test this on and use an identical, second object to actually use. But this seems kind of wasteful and I think there must be a better way….thanks everyone!

Notes:
The method which reads is not the same method that is checking for empty.
The same bufferedReader is being used throughout the object’s methods because otherwise I will need to ask the user to enter the file name again, which I don’t want to do.

  • 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-13T16:14:26+00:00Added an answer on June 13, 2026 at 4:14 pm

    Why don’t you stick to the BufferedReader idiom? Instead of asking if the file is empty, and get the next item if it’s not empty, directly ask for the next item, and have the method return null if there is no item anymore. Much simpler:

    while ((line = reader.readLine()) != null) {
        ...
    }
    

    Otherwise, you could use mark() to mark your current position, read ahead some lines, and then reset() to go back to your mark.

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

Sidebar

Related Questions

I'm trying to read values from a text file into a hashtable, I want
I'm trying to use urllib and urllib2 to read from a text file that
I am trying to read from a text file and tokenize the input. I
I am trying to read values from a text file using the Qt code
I am trying to read some info from a text file by using windows
I'm trying to read the last line from a text file. Each line starts
I am trying to read a list of values from a text file, hello.txt
I'm trying to read a list of items from a text file and format
I am trying to read a text file from raw folder, and it works
I'm trying to find and read a string from a text file using a

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.