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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:16:41+00:00 2026-05-17T21:16:41+00:00

Java IO implementation of unix/linux "tail -f" has a similar problem; but the solution

  • 0

Java IO implementation of unix/linux "tail -f" has a similar problem; but the solution is not viable for log files that generate about 50-100 lines per second.

I have an algorithm that emulates the tail functionality in Linux. For example,

File _logFile = new File("/tmp/myFile.txt");
long _filePtr = _logFile.length();

while (true)
{
     long length = _logFile.length();
     if (length < _filePtr)
     {
            // means file was truncated
     }
     else if (length > _filePtr)
     {
            // means something was added to the file
     } 
// we ignore len = _filePtr ... nothing was written to file
}

My problem is when: “something was added to the file” (referring to the else if() statement).

else if (length > _filePtr)
{
     RandomAccessFile _raf = new RandomAccessFile(_logFile, "r");
     raf.seek(_filePtr);

     while ((curLine = raf.readLine()) != null)
           myTextPane.append(curLine);

        _filePtr = raf.getFilePointer();
        raf.close();
}

The program blocks at while ((curLine = raf.readLine())…. after 15 seconds of run-time! (Note: that the program runs right for the first 15 seconds).

It appears that raf.readLine() is never hitting NULL, because I believe this log file is being written so fast that we go into an “endless cat and mouse” loop.

What’s the best way to emulate Linux’s tail?

  • 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-17T21:16:42+00:00Added an answer on May 17, 2026 at 9:16 pm

    I would think that you would be best served by grabbing a block of bytes based on the file’s length, then release the file and parse a ByteArrayInputStream (instead of trying to read directly from the file).

    So use RandomAccessFile#read(byte[]), and size the buffer using the returned file length. You won’t always show the exact end of the file, but that is to be expected with this sort of polling algorithm.

    As an aside, this algorithm is horrible – you are running IO operations in a crazy tight loop – the calls to File#length() will block, but not very much. Expect this routine to take your app to it’s knees CPU-wise. I don’t necessarily have a better solution for you (well – actually, I do – have the source application write to a stream instead of a file – but I recognize that isn’t always feasible).

    In addition to the above, you may want to introduce a polling delay (sleep the thread by 100ms each loop – it looks to me like you are displaying to a GUI – a 100ms delay won’t hurt anyone, and will greatly improve the performance of the swing operations).

    ok – final beef: You are adjusting a Swing component from what (I hope) is code not running on the EDT. Use SwingWorker#invokeLater() to update your text pane.

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

Sidebar

Related Questions

I have heard that the Java implementation of Generics is not as good as
Building on what has been written in SO question Best Singleton Implementation In Java
I am working on a BlackBerry j2me Java implementation that does not have the
What is the fastest list implementation (in java) in a scenario where the list
Is there an openID implementation in Java? I would like to use this in
I'm looking for a KDTree implementation in Java. I've done a google search and
I've been told that the java class TreeMap uses an implementation of a RB
The other day I decided to write an implementation of radix sort in Java.
Is the following java implementation of the visitor pattern using generics, general enough to
Do you know any java implementation of the Porter2 stemmer(or any better stemmer written

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.