Reading Input from another programs Console. Java
So here is what I want to do. Im currently managing a TF2(Source Engine Game) server, and 2 console are running at the same time for the server, one for the game and the other to compile stats, both of them have plenty verbose on their console, so I think "Why not create a program that filters both console text, and only leaves the important things", and maybe adding 开发者_开发百科a way to send commands also to both consoles.
Just have the the consoles redirect their output to a file:
Windows: http://blog.crankybit.com/redirecting-output-to-a-file-in-windows-batch-scripts/
*nix: http://linux.byexamples.com/archives/349/how-to-redirect-output-to-a-file-as-well-as-display-it-out/
Now your Java program can read the files and parse the output. You can even use Tailer from Apache Commons to do something whenever the file is updated.
You may want to look at the ProcessBuilder
to start the process from within your other java program. You can then attach to the process' output stream via Process.getInputStream()
.
Named pipes will probably be your best bet here. They function like a command line pipe. The difference is that there is a filename that can let you connect two separate commands on difference consoles. For instance on a *nix/MacOSX system you can do a named pipe like this:
$ mkfifo my_pipe
$ echo "information" > my_pipe
On a separate console
$ cat my_pipe
This is equivalent to:
$ echo "information" | cat
except that you can do it with separate consoles.
You can have your consoles send information to a named pipe and then run your Java code with the pipe as input. Like this.
$ console > my_pipe
On another terminal:
$ java my_java_code < my_pipe
This can also be done on Windows but I'm not sure about the specific syntax of the commands. This link may help:
Named Pipes in Windows
精彩评论