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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:17:43+00:00 2026-05-26T10:17:43+00:00

I am wondering is there is a simple way to tail a file in

  • 0

I am wondering is there is a simple way to tail a file in Groovy? I know how to read a file, but how do I read a file and then wait for more lines to be added, read them, wait, etc…

I have what I am sure is a really stupid solution:

def lNum = 0
def num= 0
def numLines = 0
def myFile = new File("foo.txt")
def origNumLines = myFile.eachLine { num++ }
def precIndex = origNumLines

while (true) {
num = 0
lNum = 0
numLines = myFile.eachLine { num++ }

if (numLines > origNumLines) {
    myFile.eachLine({ line ->

    if (lNum > precIndex) {
            println line
        }
    lNum++
    })
}
precIndex = numLines

 Thread.sleep(5000)
}

Note that I am not really interested in invoking the Unix “tail” command. Unless it is the only solution.

  • 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-26T10:17:43+00:00Added an answer on May 26, 2026 at 10:17 am

    I wrote a groovy class which resembles the basic tail functionality:

    class TailReader {
    
      boolean stop = false
    
      public void stop () {
        stop = true
      }
    
      public void tail (File file, Closure c) {
        def runnable = {
         def reader
    
          try {
            reader = file.newReader()
            reader.skip(file.length())
    
            def line
    
            while (!stop) {
              line = reader.readLine()
              if (line) {
                c.call(line)
              }
              else {
                Thread.currentThread().sleep(1000)
              }
            }
    
          }
          finally {
            reader?.close()
          }
        } as Runnable
    
        def t = new Thread(runnable)
        t.start()
      }
    }
    

    The tail method taks a file and closure as parameters. It will run in a separate thread and will pass each new line that will be appended to the file to the given closure. The tail method will run until the stop method is called on the TailReader instance. Here’s a short example of how to use the TailReader class:

    def reader = new TailReader()
    reader.tail(new File("/tmp/foo.log")) { println it }
    
    // Do something else, e.g. 
    // Thread.currentThread().sleep(30 * 1000)
    
    reader.stop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering if there is a simple way to allow a JQuery datatable
just wondering if there is a simple way in java to display the contents
I was wondering if in python there was a simple way to run code
I'm tooling around with Django and I'm wondering if there is a simple way
I'm wondering if there's a simple way for a Word macro to determine which
I was wondering if there's a simple way to parse command line options having
I'm wondering if there is a simple way to reset the values of a
Just wondering if there is a simple way to trigger a custom function each
I was wondering if there's a simple way to cancel an AJAX request? Beyond
I was wondering if there is a simple way in Perl to ensure that

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.