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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:44:21+00:00 2026-05-15T16:44:21+00:00

The task I have is to (somewhat efficiently) read line-by-line through a very large,

  • 0

The task I have is to (somewhat efficiently) read line-by-line through a very large, continuously growing file. Here’s basically what I’m doing now:

BufferedReader rd = //initialize BufferedReader
String line;
while(true){
    while((line=rd.readLine())==null){
        try{
            Thread.sleep(1000);
        }catch(InterruptedException e){
            //handle exception
        }
    }
    //process line
}

So my BufferedReader just hangs at the end of the file until there is more stuff to be read. This works pretty well, but there’s one problem – If readLine is called while the process that writes to the file is in the middle of writing a line. Then the first call to readLine will return the first section of the line, and the next call will return the second section. But I really need those two parts together, I need complete lines.

More specifically, my problem happens when the following interleaving of events happens:

  1. Writing process writes most of a line
  2. readLine() is called
  3. Writing process finishes that line and adds a newline character
  4. readLine() is called

The result is that each readLine() picks up a section of the entire line that the writing process is outputting. It is behaving as expected in doing so because each time it is called, it reaches the end of the file so returns what it has read.

So essentially the functionality I need is a BufferedReader that returns null one readLine earlier; one that does not give you a line until there is a line-break after it, not just EOF after it. So if it finds EOF, it does not return the line up to that point, it returns null, and returns that line once the file has been written to and there’s a new line after it.

I could probably implement a crude way of doing this by dealing with the FileReader more directly and essentially re-writing BufferedReader, but I don’t know much about how to do this efficiently. My implementation probably won’t be as fast as the real BufferedReader, and I’d like to avoid slowing down the program for the times when there is data to be read.

  • 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-15T16:44:22+00:00Added an answer on May 15, 2026 at 4:44 pm

    You could start with the source of BufferedReader and rewrite the
    String readLine(boolean ignoreLF) method which causes the trouble if it finds EOF before the end of line. (Unfortunatly it can’t be inhertited due to package scope)

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

Sidebar

Related Questions

I have some slow internet task to save and load file, I'd like to
I have a somewhat complex plot task in matplotlib that requires--I think--an autoscale() function
I have a slow custom data source in a SSIS Dataa Flow Task.I have
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have a task and I want to generate some code using the CodeDom.
I have a Task object that has a collection of Label objects ... in
I'm relatively new to Nant, what i'd like to do is have a task
I have a Rakefile with a Rake task that I would normally call from
I have a backroundrb scheduled task that takes quite a long time to run.
I have code where I schedule a task using java.util.Timer . I was looking

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.