public class Tailer extends Object implements Runnable
First you need to create a TailerListener
implementation
(TailerListenerAdapter
is provided for convenience so that you don't have to
implement every method).
For example:
public class MyTailerListener extends TailerListenerAdapter { public void handle(String line) { System.out.println(line); } }
You can create and use a Tailer in one of three ways:
An example of each of these is shown below.
TailerListener listener = new MyTailerListener(); Tailer tailer = Tailer.create(file, listener, delay);
TailerListener listener = new MyTailerListener(); Tailer tailer = new Tailer(file, listener, delay); // stupid executor impl. for demo purposes Executor executor = new Executor() { public void execute(Runnable command) { command.run(); } }; executor.execute(tailer);
TailerListener listener = new MyTailerListener(); Tailer tailer = new Tailer(file, listener, delay); Thread thread = new Thread(tailer); thread.setDaemon(true); // optional thread.start();
Remember to stop the tailer when you have done with it:
tailer.stop();
You can interrupt the thread a tailer is running on by calling Thread.interrupt()
.
thread.interrupt();
If you interrupt a tailer, the tailer listener is called with the InterruptedException
.
The file is read using the default charset; this can be overriden if necessary
Thread.interrupt()
TailerListener
,
TailerListenerAdapter
Constructor and Description |
---|
Tailer(File file,
Charset cset,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen,
int bufSize)
Creates a Tailer for the given file, with a specified buffer size.
|
Tailer(File file,
TailerListener listener)
Creates a Tailer for the given file, starting from the beginning, with the default delay of 1.0s.
|
Tailer(File file,
TailerListener listener,
long delayMillis)
Creates a Tailer for the given file, starting from the beginning.
|
Tailer(File file,
TailerListener listener,
long delayMillis,
boolean end)
Creates a Tailer for the given file, with a delay other than the default 1.0s.
|
Tailer(File file,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen)
Creates a Tailer for the given file, with a delay other than the default 1.0s.
|
Tailer(File file,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen,
int bufSize)
Creates a Tailer for the given file, with a specified buffer size.
|
Tailer(File file,
TailerListener listener,
long delayMillis,
boolean end,
int bufSize)
Creates a Tailer for the given file, with a specified buffer size.
|
Modifier and Type | Method and Description |
---|---|
static Tailer |
create(File file,
Charset charset,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen,
int bufSize)
Creates and starts a Tailer for the given file.
|
static Tailer |
create(File file,
TailerListener listener)
Creates and starts a Tailer for the given file, starting at the beginning of the file
with the default delay of 1.0s
|
static Tailer |
create(File file,
TailerListener listener,
long delayMillis)
Creates and starts a Tailer for the given file, starting at the beginning of the file
|
static Tailer |
create(File file,
TailerListener listener,
long delayMillis,
boolean end)
Creates and starts a Tailer for the given file with default buffer size.
|
static Tailer |
create(File file,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen)
Creates and starts a Tailer for the given file with default buffer size.
|
static Tailer |
create(File file,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen,
int bufSize)
Creates and starts a Tailer for the given file.
|
static Tailer |
create(File file,
TailerListener listener,
long delayMillis,
boolean end,
int bufSize)
Creates and starts a Tailer for the given file.
|
long |
getDelay()
Return the delay in milliseconds.
|
File |
getFile()
Return the file.
|
protected boolean |
getRun()
Gets whether to keep on running.
|
void |
run()
Follows changes in the file, calling the TailerListener's handle method for each new line.
|
void |
stop()
Allows the tailer to complete its current loop and return.
|
public Tailer(File file, TailerListener listener)
file
- The file to follow.listener
- the TailerListener to use.public Tailer(File file, TailerListener listener, long delayMillis)
file
- the file to follow.listener
- the TailerListener to use.delayMillis
- the delay between checks of the file for new content in milliseconds.public Tailer(File file, TailerListener listener, long delayMillis, boolean end)
file
- the file to follow.listener
- the TailerListener to use.delayMillis
- the delay between checks of the file for new content in milliseconds.end
- Set to true to tail from the end of the file, false to tail from the beginning of the file.public Tailer(File file, TailerListener listener, long delayMillis, boolean end, boolean reOpen)
file
- the file to follow.listener
- the TailerListener to use.delayMillis
- the delay between checks of the file for new content in milliseconds.end
- Set to true to tail from the end of the file, false to tail from the beginning of the file.reOpen
- if true, close and reopen the file between reading chunkspublic Tailer(File file, TailerListener listener, long delayMillis, boolean end, int bufSize)
file
- the file to follow.listener
- the TailerListener to use.delayMillis
- the delay between checks of the file for new content in milliseconds.end
- Set to true to tail from the end of the file, false to tail from the beginning of the file.bufSize
- Buffer sizepublic Tailer(File file, TailerListener listener, long delayMillis, boolean end, boolean reOpen, int bufSize)
file
- the file to follow.listener
- the TailerListener to use.delayMillis
- the delay between checks of the file for new content in milliseconds.end
- Set to true to tail from the end of the file, false to tail from the beginning of the file.reOpen
- if true, close and reopen the file between reading chunksbufSize
- Buffer sizepublic Tailer(File file, Charset cset, TailerListener listener, long delayMillis, boolean end, boolean reOpen, int bufSize)
file
- the file to follow.cset
- the Charset to be used for reading the filelistener
- the TailerListener to use.delayMillis
- the delay between checks of the file for new content in milliseconds.end
- Set to true to tail from the end of the file, false to tail from the beginning of the file.reOpen
- if true, close and reopen the file between reading chunksbufSize
- Buffer sizepublic static Tailer create(File file, TailerListener listener, long delayMillis, boolean end, int bufSize)
file
- the file to follow.listener
- the TailerListener to use.delayMillis
- the delay between checks of the file for new content in milliseconds.end
- Set to true to tail from the end of the file, false to tail from the beginning of the file.bufSize
- buffer size.public static Tailer create(File file, TailerListener listener, long delayMillis, boolean end, boolean reOpen, int bufSize)
file
- the file to follow.listener
- the TailerListener to use.delayMillis
- the delay between checks of the file for new content in milliseconds.end
- Set to true to tail from the end of the file, false to tail from the beginning of the file.reOpen
- whether to close/reopen the file between chunksbufSize
- buffer size.public static Tailer create(File file, Charset charset, TailerListener listener, long delayMillis, boolean end, boolean reOpen, int bufSize)
file
- the file to follow.charset
- the character set to use for reading the filelistener
- the TailerListener to use.delayMillis
- the delay between checks of the file for new content in milliseconds.end
- Set to true to tail from the end of the file, false to tail from the beginning of the file.reOpen
- whether to close/reopen the file between chunksbufSize
- buffer size.public static Tailer create(File file, TailerListener listener, long delayMillis, boolean end)
file
- the file to follow.listener
- the TailerListener to use.delayMillis
- the delay between checks of the file for new content in milliseconds.end
- Set to true to tail from the end of the file, false to tail from the beginning of the file.public static Tailer create(File file, TailerListener listener, long delayMillis, boolean end, boolean reOpen)
file
- the file to follow.listener
- the TailerListener to use.delayMillis
- the delay between checks of the file for new content in milliseconds.end
- Set to true to tail from the end of the file, false to tail from the beginning of the file.reOpen
- whether to close/reopen the file between chunkspublic static Tailer create(File file, TailerListener listener, long delayMillis)
file
- the file to follow.listener
- the TailerListener to use.delayMillis
- the delay between checks of the file for new content in milliseconds.public static Tailer create(File file, TailerListener listener)
file
- the file to follow.listener
- the TailerListener to use.protected boolean getRun()
public long getDelay()
public void run()
public void stop()
Copyright © 2002–2016 The Apache Software Foundation. All rights reserved.