java 如何清除java中的输入流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10002189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:12:36  来源:igfitidea点击:

How to clear an inputstream in java

java

提问by ysdelahoz

I have data coming from a bluetooth device, the data is being stored in an inputstream. I am taking the data from the inputstream and generating a graphic with Jfreechart. Whenever I turn off the bluetooth device the data keeps coming from the inputstream and the graphic continues being generated.

我有来自蓝牙设备的数据,数据存储在输入流中。我从输入流中获取数据并使用 Jfreechart 生成图形。每当我关闭蓝牙设备时,数据就会不断来自输入流,并且图形会继续生成。

I need the data and the graphic to stop when I turn off the bluetooth device.

当我关闭蓝牙设备时,我需要数据和图形停止。

I am using Java.

我正在使用 Java。

回答by Petr Jane?ek

Every InputStream has a close()method that should do exactly what you need ... if you can detect that the device is turned off, that is.

每个 InputStream 都有一个close()方法可以完全满足您的需求……如果您可以检测到设备已关闭,那就是。

The docs on this.

关于这个的文档。

回答by Parthasarathy B

Reference link : Closing java InputStream

参考链接:关闭java InputStream

which resolved my problem too.

这也解决了我的问题。

    Properties props = new Properties();
    InputStream fis = new FileInputStream("message.properties");
    try {
        props.load(fis);
        //omitted.
    } catch (Exception ex) {
        //omitted.
    } finally {
        try {
            fis.close();
            fis=null;
        } catch (IOException ioex) {
            //omitted.
        }

}