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

  • Home
  • SEARCH
  • 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 8403071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:14:48+00:00 2026-06-09T22:14:48+00:00

I’m trying to implement a log tailer that will continue tailing after the previous

  • 0

I’m trying to implement a log tailer that will continue tailing after the previous file has been deleted and replaced by a new one (forever). So, I don’t have to run this program every time we restart the server (that prints out log).

This is my code.

public void tail(String file) throws InterruptedException, IOException {
    waitUntilFileExist(file);

    File f = new File(file);

    BufferedReader br = new BufferedReader(new FileReader(file.trim()));

    br.skip(f.length());
    String line = null;

    while (f.exists()) {

       f = new File(file);
        line = br.readLine();

        if (line == null) {

            Thread.sleep(1000);

        } else {
            if (msl != null) {
               //Send line to attached interface
                msl.onMessage(line);
            }
        }

    }

   waitUntilFileExist(file);

    tail(file);


}

public void waitUntilFileExist(String file) throws InterruptedException {
    File f = new File(file);
    if (!f.exists()) {
        System.out.println("File doesn't exists");
        Thread.sleep(1000);
        waitUntilFileExist(file);
    } else {
        System.out.println("File does exists");
    }
}

First, it will check whether the specified file exists? and while it does exists, read the file and print the line. If the file is deleted while it’s reading, it will wait until the file is re-created and then call the tail method.

I test this out on a Linux box and it works fine but i’m not sure if this is the right way to do it.
Do you have any alternative way to do this?
Will this works for a production environment like days and months?

  • 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-09T22:14:50+00:00Added an answer on June 9, 2026 at 10:14 pm

    Your program will fail with stack overflow error because every time you go deeper and deeper with recursive method calls in both methods tail and waitUntilFileExist, and never get out from them.
    I make better variant of your code:

    public void tail(String file) throws InterruptedException, IOException {
        boolean isNeedRun = true;
        while (isNeedRun) {
            waitUntilFileExist(file);
            try {
                readFile(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    private void readFile(String file) throws InterruptedException, IOException {
        File f = new File(file);
    
        FileReader reader = new FileReader(file.trim());
        try {
            BufferedReader br = new BufferedReader(reader);
            br.skip(f.length());
            String line;
    
            while (f.exists()) {
    
                f = new File(file);
                line = br.readLine();
    
                if (line == null) {
    
                    Thread.sleep(1000);
    
                } else {
                    if (msl != null) {
                        //Send line to attached interface
                        msl.onMessage(line);
                    }
                }
            }
        } finally {
            try {
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    private void waitUntilFileExist(String file) throws InterruptedException {
        File f = new File(file);
        while (!f.exists()) {
            System.out.println("File doesn't exists");
            Thread.sleep(1000);
        }
        System.out.println("File does exists");
    }
    

    But this variant also not perfect! You may loose some lines because can be situation when something will be written to file between lines waitUntilFileExist(file); and br.skip(f.length());.

    If you need tail on unix system it is better to use tail console tool. You can start separate process from java and consume it’s output. This variant will guarantee 0% loses when file is deleted and created again.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is

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.