I need to wrap the Unix command "tail -f" in a BufferedInputStream. I don't want to simulate or mimic tail as stated by this question. Rather, I want to use tail, waiting for it to give me a new line.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
check also ProcessBuilder:
where
file
is String like "/var/log/messages".Your best bet is to use the
Process
class and read with aScanner
:hasNextLine()
should block as it's waiting for more input from the input stream, so you will not be busy-waiting as data comes in.I am guessing that system() and popen() type approaches will not work as they will block your program until the tail command terminates.
I think you could redirect the output to a file and use 'diff' against the last version to see which lines are new?
Look at Runtime.exec(String command). Returns a Process object that has Input and Output Streams.
If you have the unix command
Then the tail would appear as an
InputStream
that may block for a period of time. If you don't want to block yourself, you would use the nio packages. I believe that most other ways to access the tail command (such asProcess
) results in a similarInputStream
.Two more full blown projects:
http://www.eclipse.org/tptp/home/documents/tutorials/gla/gettingstarted_gla.html
http://commons.apache.org/io/api-release/org/apache/commons/io/input/Tailer.html