public class Tailer
extends java.lang.Object
implements java.lang.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:
Executor
Thread
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 overridden if necessary
Thread.interrupt()
TailerListener
,
TailerListenerAdapter
Modifier and Type | Field and Description |
---|---|
private java.nio.charset.Charset |
cset
The character set that will be used to read the file.
|
private static int |
DEFAULT_BUFSIZE |
private static java.nio.charset.Charset |
DEFAULT_CHARSET |
private static int |
DEFAULT_DELAY_MILLIS |
private long |
delayMillis
The amount of time to wait for the file to be updated.
|
private boolean |
end
Whether to tail from the end or start of file
|
private java.io.File |
file
The file which will be tailed.
|
private byte[] |
inbuf
Buffer on top of RandomAccessFile.
|
private TailerListener |
listener
The listener to notify of events when tailing.
|
private static java.lang.String |
RAF_MODE |
private boolean |
reOpen
Whether to close and reopen the file whilst waiting for more input.
|
private boolean |
run
The tailer will run as long as this value is true.
|
Constructor and Description |
---|
Tailer(java.io.File file,
java.nio.charset.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(java.io.File file,
TailerListener listener)
Creates a Tailer for the given file, starting from the beginning, with the default delay of 1.0s.
|
Tailer(java.io.File file,
TailerListener listener,
long delayMillis)
Creates a Tailer for the given file, starting from the beginning.
|
Tailer(java.io.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(java.io.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(java.io.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(java.io.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(java.io.File file,
java.nio.charset.Charset charset,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen,
int bufSize)
Creates and starts a Tailer for the given file.
|
static Tailer |
create(java.io.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(java.io.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(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end)
Creates and starts a Tailer for the given file with default buffer size.
|
static Tailer |
create(java.io.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(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen,
int bufSize)
Creates and starts a Tailer for the given file.
|
static Tailer |
create(java.io.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.
|
java.io.File |
getFile()
Return the file.
|
protected boolean |
getRun()
Gets whether to keep on running.
|
private long |
readLines(java.io.RandomAccessFile reader)
Read new lines.
|
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.
|
private static final int DEFAULT_DELAY_MILLIS
private static final java.lang.String RAF_MODE
private static final int DEFAULT_BUFSIZE
private static final java.nio.charset.Charset DEFAULT_CHARSET
private final byte[] inbuf
private final java.io.File file
private final java.nio.charset.Charset cset
private final long delayMillis
private final boolean end
private final TailerListener listener
private final boolean reOpen
private volatile boolean run
public Tailer(java.io.File file, TailerListener listener)
file
- The file to follow.listener
- the TailerListener to use.public Tailer(java.io.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(java.io.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(java.io.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(java.io.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(java.io.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(java.io.File file, java.nio.charset.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(java.io.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(java.io.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(java.io.File file, java.nio.charset.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(java.io.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(java.io.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(java.io.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(java.io.File file, TailerListener listener)
file
- the file to follow.listener
- the TailerListener to use.public java.io.File getFile()
protected boolean getRun()
public long getDelay()
public void run()
run
in interface java.lang.Runnable
public void stop()
private long readLines(java.io.RandomAccessFile reader) throws java.io.IOException
reader
- The file to readjava.io.IOException
- if an I/O error occurs.