Java - I/O: Low Level and High Level Streams

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

JAVA –I/O

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.

Low-Level and High-Level Streams:


Low-level streams: An IO stream that connects directly to a data source,
such as a file or socket. The low-level streams take in actual data sources in
their constructors.
FileOutputStream and each of its constructors takes in a variation of a
filename.
Some more example
 FileInputStream and FileOutputStream
 ByteArrayInputStream and ByteArrayOutputStream.
 PipedInputStream and PipedOutputStream

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

Low-Level and High-Level Readers and Writers :


Low-Level Readers/Writers : The low-level readers and writers connect
directly to a data source, similarly to memory or a file.
 CharArrayReader and CharArrayWriter. For reading from and writing to
arrays of characters.
 FileReader and FileWriter. For reading from and writing to files
containing character data.
 PipedReader and PipedWriter. For creating character streams between two
threads.
 StringReader and StringWriter. For reading from and writing to String
objects.

High-Level Readers/Writers : the high-level readers and writers connect to


existing readers and writers.
 BufferedReader and BufferedWriter. For buffering the characters in the
character stream.
 InputStreamReader and OutputStreamWriter. For converting between byte
streams and character streams.
 PrintWriter. For printing text to either an output stream or a Writer.
System.out is a PrintWriter object.
 PushbackReader. For readers that allow characters to be read and then
pushed back into the stream.

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!

Things you can do with a File object

 Make a File object representing an existing file


o File f = new File(“MyCode.txt”);
 Make a new directory
File dir = new File(“Chapter7”);
dir.mkdir();
 List the contents of a directory
if (dir.isDirectory()) {
String[] dirContents = dir.list();
for (int i = 0; i < dirContents.length; i++) {
System.out.println(dirContents[i]);
}
}
 Get the absolute path of a file or directory
o System.out.println(dir.getAbsolutePath());
 Delete a file or directory (returns true if successful.
o boolean isDeleted = f.delete();

You might also like