Chapter 3
Chapter 3
File Handling
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.
System.out.println("simple message" );
System.err.println("error message" );
Let's see the code to get input from console.
int i=System.in.read();//returns ASCII code of 1st character
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.
OutputStream Hierarchy
InputStream class
InputStream class is an abstract class. It is the superclass of all classes representing an input
stream of bytes.
InputStream Hierarchy
FileOutputStream Class
Java FileOutputStream is an output stream used for writing data to a file.
If you have to write primitive values into a file, use FileOutputStream class. You can write byte-
oriented as well as character-oriented data through FileOutputStream class. But, for character-
oriented data, it is preferred to use FileWriter than FileOutputStream.
FileOutputStream class declaration
Let's see the declaration for Java.io.FileOutputStream class:
public class FileOutputStream extends OutputStream
FileOutputStream Example 1: write byte
import java.io.FileOutputStream;
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt" );
fout.write(65);
fout.close();
System.out.println("success..." );
}catch(Exception e){System.out.println(e);}
}
}
Output:
Success...
The content of a text file testout.txt is set with the data A.
testout.txt
A
FileInputStream Class
Java FileInputStream class obtains input bytes from a file. It is used for reading byte-oriented
data (streams of raw bytes) such as image data, audio, video etc. You can also read character-
stream data. But, for reading streams of characters, it is recommended to use FileReader class.
Java FileInputStream class declaration
Let's see the declaration for java.io.FileInputStream class:
public class FileInputStream extends InputStream
FileInputStream example 1: read single character
import java.io.FileInputStream;
public class DataStreamExample {
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("D:\\testout.txt" );
int i=fin.read();
System.out.print((char)i);
fin.close();
}catch(Exception e){System.out.println(e);}
}
}
Note: Before running the code, a text file named as "testout.txt" is required to be created. In this
file, we are having following content:
Welcome to javatpoint.
After executing the above program, you will get a single character from the file which is 87 (in
byte form). To see the text, you need to convert it into character.
Output:
W
Java FileInputStream example 2: read all characters
package com.javatpoint;
import java.io.FileInputStream;
public class DataStreamExample {
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("D:\\testout.txt" );
int i=0;
while((i=fin.read())!=-1){
System.out.print((char)i);
}
fin.close();
}catch(Exception e){System.out.println(e);}
}
}
Output:
Welcome to javaTpoint
FilterReader
Java FilterReader is used to perform filtering operation on reader stream. It is an abstract class
for reading filtered character streams.
The FilterReader provides default methods that passes all requests to the contained stream.
Subclasses of FilterReader should override some of its methods and may also provide additional
methods and fields.
import java.io.*;
class filereader
{
public static void main(String args[])throws Exception
{
try
{
FileReader fin = new FileReader("D:\\sample.txt");
int i=fin.read();
while(i!=-1)
{
System.out.println((char)i);
i=fin.read();
}
fin.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
FilterWriter
Java FilterWriter class is an abstract class which is used to write filtered character streams.
The sub class of the FilterWriter should override some of its methods and it may provide
additional methods and fields also.
import java.io.*;
class filewriter
{
public static void main(String args[])throws Exception
{
try
{
FileReader fin = new FileReader("D:\\sample.txt");
FileWriter fout = new FileWriter("D:\\Test1.txt");
int i=fin.read();
while(i!=-1)
{
fout.write((char)i);
i=fin.read();
}
System.out.println("File Copied succesfully");
fin.close();
fout.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
********