Java BufferedReader对字符的操作? [英] Java BufferedReader action on character?

查看:122
本文介绍了Java BufferedReader对字符的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此处输入代码我正在从Linux中的设备读取流,其中包含十六进制字母,并以 ^ M定界。只要设备准备好获取更多信息,它就会发送字符>。流看起来像这样:

enter code hereI'm reading a stream from a device in Linux which contains hexidecimal letters and is delimited by "^M". Whenever the device is ready for more information it sends the character ">". The stream looks like this:

^M^M01 02 F3^M00 01 F3 3E^M>"

我需要检测char>并对该char执行操作。有没有办法我可以在>上定界并读取了该字符?

I need to detect the char > and perform an action on that char. Is there a way I can delimit on ">" and have that character read?

目前基本上看起来像这样

Basically it currently looks like this

 01 02 F3
00 01 F3 3E

我需要>触发单独的操作。
如果由于某种原因仅存在一个char字符,它不会触发读取缓冲区。我正在考虑^ M。我需要>做其他事情。

I need > to trigger a separate action. It does not trigger the read buffer if just a single char is in there for some reason. I'm delmiting on ^M. I need > to do something else.

推荐答案

这很有可能。我最近编写了一个项目,该项目使用非常相似的界面读取了多只鼠标的输入,这些鼠标的二进制数据通过文件I /

It's quite possible to do this. I recently wrote a project which read input from multiple mice using a very similar interface. The binary data from the mice came in via file I/O.

我认为这里的缓冲I / O是不合适的,出于性能方面的考虑,缓冲I / O会尝试为您填充缓冲区。将花费大部分时间ime等待新角色。如果您不批量阅读,则原始字节读取InputStream将达到目的:它们将在收到请求的/预期的字节数(在您的情况下为1)后立即返回。在您的情况下,在读取> 字符后,您将为以下字符发出另一个单字节读取。

I think buffered I/O is inappropriate here. Buffered I/O will try to fill a buffer for you, for performance reasons. But it seems that your app will be spending most of its time waiting for new characters. If you're not reading in bulk, then raw byte reads InputStream will do the trick: They will return immediately after the requested/expected number of bytes (1 in your case) have been received. In your case, after reading the > character, you'd issue another single-byte read for the following char.

您需要弄清楚如何处理中间字符。处理完> 和以下字符后,您要如何处理后面所有非

You'll need to figure out what to do with intervening characters. Once you've processed your > and the following char, what do you want to do with any following characters that are not >?

我在应用程序中所做的就是直接从 InputStream 中读取单个字节。这将阻止,因此必须在单独的 Thread 中完成,该线程花费大量时间等待。当第一个字符出现时,我知道要阅读一些更多的内容(就像您会读的一样),然后我就可以对其进行处理了。我设置了某种队列来将该信息传递到我的应用程序的其余部分-您必须对线程间通信进行某种同步。

What I did in my app was to directly read single bytes from an InputStream. This would block, so it had to be done in a separate Thread that spent most of its time waiting. When the first character came in, I knew to read a handful more (just like you will), which I could then process. I had some kind of queue set up for passing this information to the rest of my app - you'll have to work out some kind of synchronization for your inter-thread communication.

编辑:进行多次小的编辑以阐明要点。抱歉,如果最终导致混淆。

Multiple small edits to clarify points. Sorry if this ends up confusing.

这篇关于Java BufferedReader对字符的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆