tailコマンドを使ってjava経由でtailもどき 2008/02/02

tailもどきをjavaで実装するのが面倒なら、はじめからコマンドを使うってのもありかもしれない。

windowsならcygwinにあるのを使えば、たいていのosにあると思われるtailコマンドを使えるわけなのですな。

processを使って標準入力を受けたものをそのまま標準出力しています。

import java.io.IOException;
import java.io.InputStream;

public class A_TAIL {

/**
* @param args
*/
public static void main(String[] args) {

String command = "/usr/bin/tail -f /Users/xxxx/tmp/z.txt";
exec(command);

}

protected static boolean exec(String command) {

Process process = null;

try {

Runtime runtime = Runtime.getRuntime();
process = runtime.exec(command);
InputStream in = process.getInputStream();
int c;
while ((c = in.read()) != -1) {
System.out.print((char) c);
}

} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (process != null) {
try {
process.getErrorStream().close();
} catch (IOException e) {
}
try {
process.getInputStream().close();
} catch (IOException e) {
}
try {
process.getOutputStream().close();
} catch (IOException e) {
}
}
}

return true;
}
}

: