3

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

4
  • 1
    How are you actually initializing bsr? You're certainly not doing it the way you say you are. Commented Jun 10, 2010 at 18:00
  • What do you get as the output of "ls -l /dev/ap"? Commented Jun 10, 2010 at 19:19
  • BufferedReader bsr = new BufferedReader(new FileReader("/dev/ap") Commented Jun 11, 2010 at 10:53
  • ls -l /dev/ap shows that it is a link to /dev/ttyaa02 Commented Jun 11, 2010 at 10:55

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Apparently, need to treat this dev as a serial port which requires javacomm.
@Jim Jones: It's never easy. :-) I've added some links above.
Yah in fact, I think I'll have to use RXTX because it is a virtual serial port.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.