匿名 #7081(2008/08/09 21:26 GMT) [ Java ] Rating0/0=0.00
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
import java.io.*; public class Tail { private static final int MAX_LINE_LENGTH = 1000; private static final long SLEEP_TIME = 500; // 500 m sec private static int numOfLine = 10; private static boolean fOption = false; private static String fileName = null; private static InputStream targetStream = System.in; public static void main(String[] args) throws Exception { for (String s: args) { if ("-help".equals(s)) { System.err.println("Usage> java Tail [-f] [-<num>] [filename]"); return; } else if ("-f".equals(s)) fOption = true; else if (s.startsWith("-")) numOfLine = -Integer.parseInt(s); else fileName = s; } if (fileName != null) { File target = new File(fileName); long len = target.length(); targetStream = new FileInputStream(target); len -= MAX_LINE_LENGTH * numOfLine; if (len > 0) targetStream.skip(len); } BufferedReader br = new BufferedReader(new InputStreamReader(targetStream)); String line; String[] lines = new String[numOfLine]; int ip = 0; while ((line = br.readLine()) != null) { lines[ip] = line; if (++ip >= numOfLine) ip = 0; } int i = ip; do { if (lines[i] != null) System.out.println(lines[i]); if (++i >= numOfLine) i = 0; } while (i != ip); if (fOption && fileName != null) { while (true) { Thread.sleep(SLEEP_TIME); line = br.readLine(); if (line != null) System.out.println(line); } } } }
Rating0/0=0.00-0+
[ reply ]
匿名
#7081()
[
Java
]
Rating0/0=0.00
起動方法は
java Tail [-f] [-<num>] [<ファイル名>]
-f: ファイル末尾まで読み込んでも終了せず、ファイルが成長した部分をポーリングで表示する(ファイル名を与えない場合は無視する)。
num: ファイル末尾からnum行を表示する(デフォルトは10行)。
ファイル名: 表示するファイル名。省略した場合は標準入力。
です。
Rating0/0=0.00-0+
[ reply ]