开发者

Reading data from a unix device

On one of my RedHat Linux Servers a couple devices have been setup. /dev/ap and /dev/reuter. They are AP and Reuters news feeds. At the unix command line I can do "cat /dev/ap" and it waits until a message comes down the feed and prints it out to stdout. As soon as there is a pause in the stream cat completes. I tried "more" and got the same results, same with less -f (complete msg, could be luck) and tail -f had no output in an hour.

I know these are streams, but when I try to open a Java BufferReader on new Reader("/dev/ap") I get no output. Using the following run method:

public void run() {
    String line = null;
    while(true) {
        try {
            while((line = bsr.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}  

Couple questions:

1. Are some of the unix commands limited to opening streams from files? e.g. tail? 2. What Am I doing wrong on the Java side that I can't capture the output? Wrong stream type, 开发者_如何学编程wrong wrapper type? Jim


Your approach seems sound, but readLine() is very particular about what constitutes a line of text. You might look at the raw stream:

cat /dev/ap | hexdump -C

You could also try read(), as seen in this fragment that reads from /dev/zero:

BufferedReader in = new BufferedReader(new FileReader("/dev/zero"));
for (int i = 0; i < 10; i++) {
    System.out.print(in.read() + " ");
}
System.out.println();
in.close();

Addendum: For serial I/O, consider RXTX or similar libraries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜