Java Input Output tutorial - contributed by Pradip Patil
Java Input Output
This chapter provide information about a package known as “java.io” package
which is a predefined package available in java. It provides information about
the input and output. Data is accessed from the input sources and the result of
a program is sent to the destination. The io package consists of the numerous
classes and interfaces.
Some of the classes are as follows
These are the some of the classes and interfaces which are most frequently used.
Stream: stream base i/o are built upon 4 abstract classes :
InputStream, OutputStream, Reader, Writer. Although your program performs its
i/o operation through concrete subclasses, top level classes define basic
functionality common to all stream class.
Byte class provides environment for handling byte related i/o including binary
data. Bytestream can be used with any type of object. Byte stream class are
capped by InputStream and OutputStream.
InputStream: It is an abstract class and all the method of this
class throws IOException
Some of the methods of InputStream class are as follows.
|int available ()
|Void mark(int numBytes)
||Int read(byte buffer)
|Int read(byte buffer,int offset,int numBytes)
OutputStream: It is an abstract class and all the methods of
this class throws IOException
Some of the methods of OutputStream classes are as follows.
|Void write(int b)
||Void write(byte buffer)
|Void write(byte buffer,int offset,int numBytes)
Buffered reader / writer
BufferedReader class provides Buffered character reader class. BufferedReader
class provides readLine() method.
Some of the methods of BufferedReader classes are as follows.
||Void mark(int readAheadLimit)
Public static void main(String
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter class contain following methods
||Void write(int c)
|Void write(String s,int off,int len)
||Void write(char cbuf,int off , int len)
FileReader class creates Reader that you can use to read contents of the file.
FileReader class contains following methods
|abstract void close()
||Abstract void flush()
|Void write(int ch)
||Void write(char buffer)
|Void write(String str)
||Void write(String str ,int offset,int numchars)
FileWriter class creates Writer that you can use to write to a file. It creates
the file before opening it for output.
Printwriter is a version of printstream. It provides some output methods
viz.print(), println(), PrintWriter().
PrintWriter object supports the print() and Println() for all types. PrintWriter
method calls the objects to StringMethod and print the method.
Some of the methods of printWriter class are :
|PrintWriter append(char c)
|Void print(char c)
|Void write(int c)
||Void write(String s)
Random access file
RandomAccessFile is the predefined class available in Java. In this class you
can move the file using seek() method. That’s why it is known as random access
RandomAccessFile have following access specifies
r- file is in read mode but not write
w-file open in read write mode
rws-file open in read write mode and synchronized and stored device.
To set the current position in the file and to point the position seek() method
is used .