How to clear an inputstream in java

Go To StackoverFlow.com

2

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.

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

I am using Java.

2012-04-03 22:11
by ysdelahoz


2

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.

The docs on this.

2012-04-03 22:21
by Petr Janeček
Not going to +1 or -1, but if the OP can detect the device was turned off, then he should simply stop reading the InputStream (and yes of course close it). Just closing the InputStream won't prevent his logic from continuing to attempt to read from it and will likely result in an IOException - Tim Bender 2012-04-03 22:27
Yeah, I might have been a little bit too thoughtless - Petr Janeček 2012-04-03 22:33


0

Reference link : Closing 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.
        }

}
2013-03-19 04:12
by Parthasarathy B
Ads