public class Tailer
extends java.lang.Object
implements java.lang.Runnable, java.lang.AutoCloseable
To build an instance, see Tailer.Builder.
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:
Tailer.BuilderExecutorThreadAn example of each is shown below.
TailerListener listener = new MyTailerListener(); Tailer tailer = Tailer.builder() .setFile(file) .setTailerListener(listener) .setDelayDuration(delay) .get();
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()., 2.12.0 Add Tailer.Tailable and Tailer.RandomAccessResourceBridge interfaces to tail of files accessed using
alternative libraries such as jCIFS or Apache Commons
VFS.TailerListener,
TailerListenerAdapter| Modifier and Type | Class and Description |
|---|---|
static class |
Tailer.Builder
Builds a
Tailer with default values. |
private static class |
Tailer.RandomAccessFileBridge
Bridges random access to a
RandomAccessFile. |
static interface |
Tailer.RandomAccessResourceBridge
Bridges access to a resource for random access, normally a file.
|
static interface |
Tailer.Tailable
A tailable resource like a file.
|
private static class |
Tailer.TailablePath
A tailable for a file
Path. |
| Modifier and Type | Field and Description |
|---|---|
private java.nio.charset.Charset |
charset
The character set that will be used to read the file.
|
private static java.nio.charset.Charset |
DEFAULT_CHARSET |
private static int |
DEFAULT_DELAY_MILLIS |
private java.time.Duration |
delayDuration
The amount of time to wait for the file to be updated.
|
private byte[] |
inbuf
Buffer on top of RandomAccessResourceBridge.
|
private TailerListener |
listener
The listener to notify of events when tailing.
|
private static java.lang.String |
RAF_READ_ONLY_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.
|
private Tailer.Tailable |
tailable
The file which will be tailed.
|
private boolean |
tailAtEnd
Whether to tail from the end or start of file
|
| Modifier | Constructor and Description |
|---|---|
|
Tailer(java.io.File file,
java.nio.charset.Charset charset,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen,
int bufSize)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
|
Tailer(java.io.File file,
TailerListener listener)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
|
Tailer(java.io.File file,
TailerListener listener,
long delayMillis)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
|
Tailer(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
|
Tailer(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
|
Tailer(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen,
int bufferSize)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
|
Tailer(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end,
int bufferSize)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
private |
Tailer(Tailer.Tailable tailable,
java.nio.charset.Charset charset,
TailerListener listener,
java.time.Duration delayDuration,
boolean end,
boolean reOpen,
int bufferSize)
Creates a Tailer for the given file, with a specified buffer size.
|
| Modifier and Type | Method and Description |
|---|---|
static Tailer.Builder |
builder()
Constructs a new
Tailer.Builder. |
void |
close()
Requests the tailer to complete its current loop and return.
|
static Tailer |
create(java.io.File file,
java.nio.charset.Charset charset,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen,
int bufferSize)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
static Tailer |
create(java.io.File file,
TailerListener listener)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
static Tailer |
create(java.io.File file,
TailerListener listener,
long delayMillis)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
static Tailer |
create(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
static Tailer |
create(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
static Tailer |
create(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen,
int bufferSize)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
static Tailer |
create(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end,
int bufferSize)
Deprecated.
Use
builder(), Tailer.Builder, and Tailer.Builder.get(). |
long |
getDelay()
Deprecated.
Use
getDelayDuration(). |
java.time.Duration |
getDelayDuration()
Gets the delay Duration.
|
java.io.File |
getFile()
Gets the file.
|
protected boolean |
getRun()
Gets whether to keep on running.
|
Tailer.Tailable |
getTailable()
Gets the Tailable.
|
private long |
readLines(Tailer.RandomAccessResourceBridge reader)
Reads new lines.
|
void |
run()
Follows changes in the file, calling
TailerListener.handle(String) with each new line. |
void |
stop()
Deprecated.
Use
close(). |
private static final int DEFAULT_DELAY_MILLIS
private static final java.lang.String RAF_READ_ONLY_MODE
private static final java.nio.charset.Charset DEFAULT_CHARSET
private final byte[] inbuf
private final Tailer.Tailable tailable
private final java.nio.charset.Charset charset
private final java.time.Duration delayDuration
private final boolean tailAtEnd
private final TailerListener listener
private final boolean reOpen
private volatile boolean run
@Deprecated
public Tailer(java.io.File file,
java.nio.charset.Charset charset,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen,
int bufSize)
builder(), Tailer.Builder, and Tailer.Builder.get().file - the file to follow.charset - 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 size@Deprecated
public Tailer(java.io.File file,
TailerListener listener)
builder(), Tailer.Builder, and Tailer.Builder.get().file - The file to follow.listener - the TailerListener to use.@Deprecated
public Tailer(java.io.File file,
TailerListener listener,
long delayMillis)
builder(), Tailer.Builder, and Tailer.Builder.get().file - the file to follow.listener - the TailerListener to use.delayMillis - the delay between checks of the file for new content in milliseconds.@Deprecated
public Tailer(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end)
builder(), Tailer.Builder, and Tailer.Builder.get().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.@Deprecated
public Tailer(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen)
builder(), Tailer.Builder, and Tailer.Builder.get().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 chunks@Deprecated
public Tailer(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end,
boolean reOpen,
int bufferSize)
builder(), Tailer.Builder, and Tailer.Builder.get().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 chunksbufferSize - Buffer size@Deprecated
public Tailer(java.io.File file,
TailerListener listener,
long delayMillis,
boolean end,
int bufferSize)
builder(), Tailer.Builder, and Tailer.Builder.get().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.bufferSize - Buffer sizeprivate Tailer(Tailer.Tailable tailable, java.nio.charset.Charset charset, TailerListener listener, java.time.Duration delayDuration, boolean end, boolean reOpen, int bufferSize)
tailable - the file to follow.charset - the Charset to be used for reading the filelistener - the TailerListener to use.delayDuration - 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 chunksbufferSize - Buffer sizepublic static Tailer.Builder builder()
Tailer.Builder.Tailer.Builder.@Deprecated public static Tailer create(java.io.File file, java.nio.charset.Charset charset, TailerListener listener, long delayMillis, boolean end, boolean reOpen, int bufferSize)
builder(), Tailer.Builder, and Tailer.Builder.get().file - the file to follow.charset - the character set to use for reading the file.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 chunks.bufferSize - buffer size.@Deprecated public static Tailer create(java.io.File file, TailerListener listener)
builder(), Tailer.Builder, and Tailer.Builder.get().file - the file to follow.listener - the TailerListener to use.@Deprecated public static Tailer create(java.io.File file, TailerListener listener, long delayMillis)
builder(), Tailer.Builder, and Tailer.Builder.get().file - the file to follow.listener - the TailerListener to use.delayMillis - the delay between checks of the file for new content in milliseconds.@Deprecated public static Tailer create(java.io.File file, TailerListener listener, long delayMillis, boolean end)
builder(), Tailer.Builder, and Tailer.Builder.get().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.@Deprecated public static Tailer create(java.io.File file, TailerListener listener, long delayMillis, boolean end, boolean reOpen)
builder(), Tailer.Builder, and Tailer.Builder.get().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 chunks.@Deprecated public static Tailer create(java.io.File file, TailerListener listener, long delayMillis, boolean end, boolean reOpen, int bufferSize)
builder(), Tailer.Builder, and Tailer.Builder.get().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 chunks.bufferSize - buffer size.@Deprecated public static Tailer create(java.io.File file, TailerListener listener, long delayMillis, boolean end, int bufferSize)
builder(), Tailer.Builder, and Tailer.Builder.get().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.bufferSize - buffer size.public void close()
close in interface java.lang.AutoCloseable@Deprecated public long getDelay()
getDelayDuration().public java.time.Duration getDelayDuration()
public java.io.File getFile()
java.lang.IllegalStateException - if constructed using a user provided Tailer.Tailable implementationprotected boolean getRun()
public Tailer.Tailable getTailable()
private long readLines(Tailer.RandomAccessResourceBridge reader) throws java.io.IOException
reader - The file to readjava.io.IOException - if an I/O error occurs.public void run()
TailerListener.handle(String) with each new line.run in interface java.lang.Runnable@Deprecated public void stop()
close().