- Always remember to close the Streams, so the best practises is to put the
close code
in final block, because the input or output file may not be opened correctly, so check if the references is null before close the streams. - Use Byte Stream only for primitive I/O, better to use more specific class (e.g. use character stream for .txt files).
- If you want to read or write characters other than bytes, the best stream to use is Character Stream which can transfer the underlying bytes to or from the local character sets.
- There are general-purpose Character Streams, InputStreamReader and OutputStreamWriter, which could be used in socket scinario, and also specific ones like FileReader and FileWriter used for file formats.
- For line-oriented purpose, we can use BufferedReader and PrintWriter.
- Unbuffered I/O triggers disk access, network activities for every read or write operation which cost expensively.
- Buffered I/O Streams read or write data through memory and the native operation only be triggered when the memory is empty or full.
- Sometimes we need to break the input into tokens or output something with specific format, this is the place where the Scanning and Formatting kick in.
- By default, scanner uses white space (e.g. blank, tab, line termination) to separate tokens.
- Sometimes, we need identify individual tokens, like numerics, scanner supporting identifying all numeric formats, but we need to specify the
local
.
- Console class is a alternative way for System.in and System.out, but it's convenient for the password input scenario, because it can suppress the echoing of the password.
- Data Stream is used for binary I/O, it provides the method for input and output of primitive data types and String.