|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.io.input.Tailer
public class Tailer
Simple implementation of the unix "tail -f" functionality.
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); } }
Executor
Thread
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();
TailerListener
,
TailerListenerAdapter
Field Summary | |
---|---|
private long |
delay
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 TailerListener |
listener
The listener to notify of events when tailing. |
private boolean |
run
The tailer will run as long as this value is true. |
Constructor Summary | |
---|---|
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 delay)
Creates a Tailer for the given file, starting from the beginning. |
|
Tailer(java.io.File file,
TailerListener listener,
long delay,
boolean end)
Creates a Tailer for the given file, with a delay other than the default 1.0s. |
Method Summary | |
---|---|
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 delay)
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 delay,
boolean end)
Creates and starts a Tailer for the given file. |
long |
getDelay()
Return the delay. |
java.io.File |
getFile()
Return the file. |
private java.lang.String |
readLine(java.io.RandomAccessFile reader)
Version of readline() that returns null on EOF rather than a partial line. |
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private final java.io.File file
private final long delay
private final boolean end
private final TailerListener listener
private volatile boolean run
Constructor Detail |
---|
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 delay)
file
- the file to follow.listener
- the TailerListener to use.delay
- the delay between checks of the file for new content in milliseconds.public Tailer(java.io.File file, TailerListener listener, long delay, boolean end)
file
- the file to follow.listener
- the TailerListener to use.delay
- 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.Method Detail |
---|
public static Tailer create(java.io.File file, TailerListener listener, long delay, boolean end)
file
- the file to follow.listener
- the TailerListener to use.delay
- 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 delay)
file
- the file to follow.listener
- the TailerListener to use.delay
- 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()
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 read
java.io.IOException
- if an I/O error occurs.private java.lang.String readLine(java.io.RandomAccessFile reader) throws java.io.IOException
reader
- the input file
java.io.IOException
- if an error occurs.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |