Java - I/O: Low Level and High Level Streams
Java - I/O: Low Level and High Level Streams
Java - I/O: Low Level and High Level Streams
Almost all of the classes in the java.io package fit into one of the following
two categories:
Streams. The stream classes are for performing I/O on bytes of data.
They are child classes of OutputStream and InputStream.
Readers and Writers. The reader and writer classes are for performing
I/O on characters.
They are child classes of Reader and Writer.
These four classes are abstract and represent the common functionality among
their child classes.
Key-Concept
Every IO operation takes one LOW-level and zero and more High-level classes.
Low-level class connects to the file. So its constructor take a file type
object. High-level classes connects to the other low-level class or other High
level classes.
High-level streams: An IO stream that reads or writes to another input or output stream. The high-level
streams take in other streams.
For example DataOutputStream, and it only has one constructor, which takes in an existing output
stream:
publicDataOutputStream(OutputStream out)
Some more example:
BufferedInputStream and BufferedOutputStream.
ObjectInputStream and ObjectOutputStream
CheckedInputStream and CheckedOutputStream.
ZipInputStream and ZipOutputStream
JarInputStream and JarOutputStream
Why BufferedReader/Writer?
FileWriter writes each and every thing you pass to the file each and every
time. Every trip to the disk is costly compared to manipulating data in
memory.
By changing BufferedWriter onto a fileWriter, BufferedWriter will hold all the
data need to be written until it's full. Only when the buffer is full the
FileWriter actually be told to write to the file/disk.
If you do want to send data before the buffer is full just flush it.
bufferedWriter.flush() - Send whatever is in the buffer.
FILE
A File object represents the name and path of a file or directory on disk, but
it does NOT represent, or give you access to, the data in the file!