Java Input Output tutorial


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

File FileReader FilePermission
FileWriter BufferedInputStream BufferedOutputStream
FileInputStream FileOutputStream BufferedReader
BufferedWriter InputStream Reader
CharArrayReader CharArrayWriter DataInputStream
DataOutputStream PipedWriter PrintStream

These are the some of the classes and interfaces which are most frequently used.

Byte/Character Stream

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 close()
Void mark(int numBytes) Boolean markSupported()
Int read() Int read(byte buffer[])
Int read(byte buffer[],int offset,int numBytes) Void reset()

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 close() Void flush()
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 close() Void mark(int readAheadLimit)
Int read() String readLine()
Boolean ready() Void reset()


import java.io.*;
class Demo
{
        Public static void main(String ar[])throws Exception
        {
              BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
              System.out.println(br.readLine());
        }
}

BufferedWriter class contain following methods

Void close() Void flush()
Void newLine() 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.

Print writer

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) Append(CharSequence csq)
Void close() Void flush()
Void print(char c) Void println()
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 file .

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 .



Write your comment - Share Knowledge and Experience



 
Interview questions
Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring