从unix设备读取数据
在我的一台 RedHat Linux 服务器上已经设置了几个设备。 /dev/ap 和 /dev/reuter。它们是美联社和路透社的新闻源。在unix命令行中,我可以执行“cat /dev/ap”,它会等待,直到一条消息出现在提要中并将其打印到标准输出。一旦流中出现暂停,猫就会完成。我尝试了“更多”并得到了相同的结果,与 less -f (完整的消息,可能是运气)相同,并且 tail -f 在一个小时内没有输出。
我知道这些是流,但是当我尝试在 new Reader("/dev/ap") 上打开 Java BufferReader 时,我没有得到任何输出。使用以下运行方法:
public void run() {
String line = null;
while(true) {
try {
while((line = bsr.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
几个问题:
1. 某些unix命令是否仅限于从文件中打开流?例如尾巴? 2.我在Java端做错了什么,导致我无法捕获输出? 错误的流类型、错误的包装器类型? 吉姆
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的方法似乎不错,但是
readLine()
对于一行文本的构成非常讲究。您可以查看原始流:您也可以尝试
read()
,如从/dev/zero
读取的片段所示:附录:对于串行 I/O,请考虑RXTX 或 类似库。
Your approach seems sound, but
readLine()
is very particular about what constitutes a line of text. You might look at the raw stream:You could also try
read()
, as seen in this fragment that reads from/dev/zero
:Addendum: For serial I/O, consider RXTX or similar libraries.