문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top