Frage

I wanted to know that when we read something from InputStream after that what value is there in stream? Is it deleted or same value is retained? For eg I have one code which reads something from InputStream and prints on the screen. Here is the code:

        char[] data = new char[1024];
        while (!isInterrupted()) {
            try {
                in.read(data);  // Step 1
            } catch (IOException e) {
                continue;
            }
            msg = new String(data);
            System.out.println(msg);   // Step 2
        }

So when first time step 1 reads something and step 2 prints it, it is okay. But when again it is coming to Step 1 and IF sender has NOT sent anything then what Step 1 will do? Will it wait till it gets new value (or sender sends anything) or it will read the same value and print the same value? Or it will throw an Exception? Please help.

War es hilfreich?

Lösung

The javadoc says:

This method blocks until input data is available, end of file is detected, or an exception is thrown.

So, unless the sender closes the stream, it will block until data is available, as per the documentation.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top