Home‎ > ‎Applied Core Java‎ > ‎

Basic I/O

IO operations are required when you read or write data to/from  following sources or destinations.  
  1. Data Storage : Hard Drive, Tape Drive, DVD, FDD, PAN Drive or and permanent storage
  2. Network : any TCP or UDP socket connections
  3. Hardware : Console, Keyword etc.
Data are categorize into two categories
  • Text data : Logically read and write charter by charter.
  • Binary data : Read and write byte by  byte
Java IO API provides 4 base classes to read or write the data.
  1. InputStream : class or its subclasses read data from binary files
  2. OutputStream : class or its subclasses write data to binary files
  3. Reader : class or its subclasses read data from text files
  4. Write : class and its subclasses write data to text files 
When contiguous bytes are read or write from source or destination, it is called IO streams.
Figure 1 : Text/Binary data read write classes

IO Package : java.io

The java.io package contains many classes that your programs can use to read and write data. Most of the classes implement sequential access streams. The sequential access streams can be divided into two groups: those that read and write bytes and those that read and write Unicode characters. Each sequential access stream has a specialty, such as reading from or writing to a file, filtering data as its read or written, or serializing an object.

One class, RandomAccessFile, implements random input/output access to a file. An object of this type maintains a file pointer, which indicates the current location from which data will be read or to which data will be written.

Key Classes

  1. File
  2. Read Text Data

a.       Reader

b.      FileReader

c.       InputStreamReader

d.      BufferedReader

e.       Scanner

  1. Write Text Data

f.        Writer

g.      FileWriter

h.       BufferedWriter

i.         PrintWriter

  1. Read Binary Data

j.         InputStream

k.       FileInputStream

l.         BufferedInputStream

m.     ObjectInputStream

  1. Write Binary Data

n.       OutputStream

o.      FileOutputStream

p.      BufferedOutputStream

q.      ObjectOutputStream

  1. Read/Write primitive data

r.        DataInputStream

s.       DataOutputStream

  1. Primitive File Handling like C/C++

t.        RandomAccessFile

Key Interfaces

  1. Serializable

Key Exception Classes

  1. EOFException
  2. FileNotFoundException
  3. IOException

Key Methods

  1. flush
  2. close
  3. print/println method of PrintWriter class

<<Previous
| Next>>