Java Streams  «Prev 

Reading data from a Stream

Input and output is often subject to problems outside the control of the running program.
For example,
  1. disks can develop bad sectors,
  2. construction workers can drop backholes through T1 lines connecting you to the Internet,
  3. console windows can be closed by the user.
Therefore almost every method that deals with input or output can throw an IOException.
The only exception is the PrintStream class, which eats all IOExceptions and sets an error flag instead.

Let us talk about
  1. flush() and
  2. close().
When you write data out to a stream, some amount of buffering will occur, and you never know for sure exactly when the last of the data will actually be sent. You might perform many write operations on a stream before closing it, and invoking the flush() method guarantees that the last of the data you thought you had already written actually gets out to the file. Whenever you are done using a file, either reading it or writing to it, you should invoke the close() method. When you are doing file I/O, you are using expensive and limited operating system resources, and so when you are done, invoking close() will free up those resources