Java I/O (Input and Output) is used to
process the input and produce the output.
Java uses the concept
of a stream to make I/O operation fast. The java.io package contains all the
classes required for input and output operations.
We can perform file handling in Java by Java I/O API.
Stream
A stream is a
sequence of data. In Java, a stream is composed of bytes. It's called a stream
because it is like a stream of water that continues to flow.
In Java, 3 streams
are created for us automatically. All these streams are attached with the
console.
1) System.out: standard output
stream
2) System.in: standard input stream
3) System.err: standard error stream
Let's see the code to
print output and an error message to the console.
1. System.out.println("simple message");
2. System.err.println("error message");
Let's see the code to
get input from console.
1. int i=System.in.read();//returns ASCII code of 1st character
2. System.out.println((char)i);//will print the character
OutputStream vs InputStream
The explanation of OutputStream and InputStream classes are given
below:
OutputStream
Java application uses an output stream to write data to a
destination; it may be a file, an array, peripheral device or socket.
InputStream
Java application uses an input stream to read data from a source;
it may be a file, an array, peripheral device or socket.
Let's understand the working of Java OutputStream and InputStream
by the figure given below.
OutputStream class
OutputStream class is an abstract class. It is the superclass of
all classes representing an output stream of bytes. An output stream accepts
output bytes and sends them to some sink.
InputStream class
InputStream class is an abstract class. It is the superclass of
all classes representing an input stream of bytes.
Java
ByteArrayOutputStream Class
Java ByteArrayOutputStream class is used to write
common data into multiple files. In this stream, the data is written
into a byte array which can be written to multiple
streams later.
The ByteArrayOutputStream holds a copy of data and forwards it to
multiple streams.
The buffer of ByteArrayOutputStream automatically grows according
to data.
Java ByteArrayOutputStream class declaration
Let's see the declaration for Java.io.ByteArrayOutputStream class:
1. public class ByteArrayOutputStream extends OutputStream
Example of Java ByteArrayOutputStream
Let's see a simple
example of java ByteArrayOutputStream class to
write common data into 2 files: f1.txt and f2.txt.
1. package Mypack;
2. import java.io.*;
3. public class DataStreamExample {
4. public static void main(String args[])throws Exception{
5. FileOutputStream fout1=new FileOutputStream("D:\\f1.txt");
6. FileOutputStream fout2=new FileOutputStream("D:\\f2.txt");
7.
8. ByteArrayOutputStream bout=new ByteArrayOutputStream();
9. bout.write(65);
10.
bout.writeTo(fout1);
11.
bout.writeTo(fout2);
12.
13.
bout.flush();
14.
bout.close();//has no effect
15.
System.out.println("Success...");
16.
}
17.
}
Java Writer
It is an abstract class
for writing to character streams. The methods that a subclass must implement
are write(char[], int, int), flush(), and close(). Most subclasses will
override some of the methods defined here to provide higher efficiency,
functionality or both.
Java Writer Example
1. import java.io.*;
2. public class WriterExample {
3. public static void main(String[] args) {
4. try {
5. Writer w = new FileWriter("output.txt");
6. String content = "I love my country";
7. w.write(content);
8. w.close();
9. System.out.println("Done");
10.
} catch (IOException e) {
11.
e.printStackTrace();
12.
}
13.
}
14.
}
Java Reader
Java Reader is an abstract class for
reading character streams. The only methods that a subclass must
implement are read(char[], int, int) and close(). Most subclasses, however,
will overridesome of the methods to provide higher
efficiency, additional functionality, or both.
1. import java.io.*;
2. public class ReaderExample {
3. public static void main(String[] args) {
4. try {
5. Reader reader = new FileReader("file.txt");
6. int data = reader.read();
7. while (data != -1) {
8. System.out.print((char) data);
9. data = reader.read();
10.
}
11.
reader.close();
12.
} catch (Exception ex) {
13.
System.out.println(ex.getMessage());
14.
}
15.
}
16.
}
Java
FileWriter Class
Java FileWriter class is used to write character-oriented data to
a file. It is
character-oriented class which is used for file handling in java.
Unlike FileOutputStream class, you don't need to convert string
into byte array because it provides method to write
string directly.
Java FileWriter class declaration
Let's see the declaration for Java.io.FileWriter class:
1. public class FileWriter extends OutputStreamWriter
Java FileWriter Example
In this example, we
are writing the data in the file test.txt using Java FileWriter class.
1. package Mypack;
2. import java.io.FileWriter;
3. public class FileWriterExample {
4. public static void main(String args[]){
5. try{
6. FileWriter fw=new FileWriter("D:\\test.txt");
7. fw.write("Welcome to java");
8. fw.close();
9. }catch(Exception e){System.out.println(e);}
10.
System.out.println("Success...");
11.
}
12.
}
Java
FileReader Class
Java FileReader class is used to read data from the file. It
returns data in byte format like FileInputStream class.
It is character-oriented class which is used for file handling
in java.
Java FileReader class declaration
Let's see the declaration for Java.io.FileReader class:
1. public class FileReader extends InputStreamReader
Java FileReader Example
In this example, we
are reading the data from the text file test.txt using Java FileReader class.
1. package Mypack;
2.
3. import java.io.FileReader;
4. public class FileReaderExample {
5. public static void main(String args[])throws Exception{
6. FileReader fr=new FileReader("D:\\test.txt");
7. int i;
8. while((i=fr.read())!=-1)
9. System.out.print((char)i);
10.
fr.close();
11.
}
12.
}