BufferedReader,检测是否还有文本要读取 [英] BufferedReader, detecting if there is text left to read

查看:119
本文介绍了BufferedReader,检测是否还有文本要读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个线程并且每次运行时都应该检查是否有一个新行要从 BufferedReader 中读取,但它会等待等待一条线存在,从而停止整个代码。

I'm running a thread and everytime it runs, It should be checking to see if there is a new line to read from the BufferedReader although, it gets stuck waiting for a line to exist, thus halting the entire code.

if((inputLine = bufferedReader.readLine()) != null){
                System.out.println(inputLine);
                JOptionPane.showMessageDialog(null, inputLine);
}

有没有办法更好地检查<$ c中是否有文字$ c> BufferedReader 要读?

Is there a way to better check if there is text in a BufferedReader to be read?

推荐答案

不,没有简单的方法可以做到这一点。 BufferedReader 有一个 ready 调用,但这仅适用于 read 调用,而不是 readLine 调用。如果你真的想要一个 readLine ,保证不会阻止,你必须自己使用读取并维持一个 char 缓冲自己。

No, there's no easy way to do that. BufferedReader has a ready call, but that only applies to the read calls, not the readLine call. If you really want a readLine that's guaranteed not to block, you must implement it yourself using read and maintaining a char buffer yourself.

这篇关于BufferedReader,检测是否还有文本要读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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