Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java FILE – I/O

Java I/O system is used to transfer data from one place to another place.
That is from one physical device to another physical device through Java application.

Physical devices may be any peripheral device like a mouse, keyboard, computer screen, printer, image scanner, etc.

Java performs I/O (data transfer) through streams.
A stream is a continuous flow of data through a selected path.

Java defines two types of streams

1) Byte Stream

The byte stream is used to transfer data in the form of bytes (8-bit each).
There are many classes available to handle Java I/O through byte stream. But FileInputStream & FileOutputStream are the most commonly used classes that use byte stream. Therefore we are going to focus more on these two classes.

Java Programming Language File Handling Byte Stream

2) Character Stream

Character stream is used to transfer data in the form of 16-bit Unicode (Java uses Unicode to represent characters).
There are many classes available to handle Java I/O through character stream.
We are going to focus more on FileReader, FileWriter, BufferedReader, BufferedWriter & PrintWriter classes that use character stream.

Java Programming Language File Handling Character Stream

Java I/O is completely handled under java.io package.
The java.io package contains almost every class & interface required to perform I/O.

Before moving to classes related to byte stream & character stream in detail, let’s learn about very important class present in java.io package is known as File.

Generally, a file is a place where data is stored or retrieved. Therefore to operate with files, Java introduced a class named File.

File

The File class is present in java.io package.
It does not operate on streams unlike other I/O classes.
It is mainly used to create files & directories.
It is also used to search files, get their names, check their existence, etc. with the help of methods defined in it.

Following constructors are used to create file objects

Java Programming Language File Handling Constructors

Constructors Explanation

1) File(String pathName)

This constructor creates the only File object, not physical file or a physical directory.

Creation of File objects using above constructor

Example

 import java.io.File;

 class FileConstructorDemo1 
 {
     public static void main(String args[])
     {
         File f = new File("xyz.txt"); // Only File object is created
     }
 }

This constructor creates the only File object, not file.
If the file is already created, then File reference f points to the created file “xyz.txt”.

Java Programming Language Creation of File Objects

If the file is not available, the only File object is created.

Creation of file using createNewFile() method

The method createNewFile() is used to create a new file.

Example

 import java.io.File;
 import java.io.IOException;

 class FileConstructorDemo1 
 {
     public static void main(String args[]) throws IOException
     {
         File f = new File("xyz.txt"); // Only File object is created
         f.createNewFile(); // This method creates a new file with name xyz.txt
     }
 }

Java Programming Language Creation of File Objects 2

Creation of File objects for directory using above constructor

A directory contains the list of other files & directories.

Example

 import java.io.File;

 class FileConstructorDemo2 
 {
     public static void name(String args[])
     {
         File f = new File("ABC"); // File object is created
     }
 }

If a directory is already created then f points to the created directory known as “ABC”.

Java Programming Language Creation of File IO Directory 1

If a directory is not available, the only File object is created.

Creation of directory using mkdir() method

The method mkdir() is used to create a new directory.

Example

 import java.io.File;

 class FileConstructorDemo2 
 {
     public static void main(String args[])
     {
         File f = new File("ABC"); // File object is created
         f.mkdir(); // This line creates a new Directory
     }
 }

Java Programming Language Creation of File IO Directory 2

2) File(String directory, String file)

This constructor is used to create a file or directory in other directory using directory path name.

Example

 import java.io.File;
 import java.io.IOException;

 class FileConstructorDemo3 
 {
     public static void main(String args[]) throws IOException
     {
         File f = new File("ABC","xyz.txt"); // File object is created
         f.createNewFile(); // This line creates a new file in a specified Directory
     }
 }

Java Programming Language Creation of File IO Directory 3

3) File(File f, String file)

This constructor is used to create a file or directory in another directory using File reference.

Example

 import java.io.File;
 import java.io.IOException;

 class FileConstructorDemo4 
 {
     public static void main(String args[]) throws IOException
     {
         File f1 = new File("ABC"); // File object is created
         f1.mkdir(); // This line creates a new Directory
         File f2 = new File(f1, "xyz.txt"); // Another File object is created
         f2.createNewFile();  // This line creates a new file 
     }
 }

Java Programming Language Creation of File IO Directory 4

Methods defined in FILE class (CLICK HERE)


Let’s see a program using some methods.

Example

 import java.io.File;
 import java.io.IOException;

 class FileMethodsTest 
 {
     public static void main(String args[]) throws IOException
     {
         File f1 = new File("ABC");   // File object is created
         f1.mkdir();   // This line creates a new Directory
         File f2 = new File(f1, "xyz.txt");   // Another File object is created
         File f3 = new File(f1, "abc.txt");   // Another File object is created
         File f4 = new File(f1, "ijk.txt");   // Another File object is created
         f2.createNewFile();    // This line creates a new file 
         f3.createNewFile();    // This line creates a new file 
         f4.createNewFile();    // This line creates a new file 
		
         System.out.println("List of files present in directory ABC");

         for(String s:f1.list())
         {
             System.out.println(s);
         }
		
         System.out.println("Is ABC directory :: "+f1.isDirectory());
         System.out.println("Is abc.txt file :: "+f3.isFile());
         System.out.println("Is abc.txt directory :: "+f3.isDirectory());
		
         System.out.println(f4.getName());
		
         System.out.println("Does xyz.txt exists :: "+f2.exists());
     }
 }

Output

List of files present in directory ABC
abc.txt
ijk.txt
xyz.txt
Is ABC directory :: true
Is abc.txt file :: true
Is abc.txt directory :: false
ijk.txt
Does xyz.txt exists :: true

Now let’s look in detail about few very important classes working with byte stream & character stream.

Classes handling Java I/O through a byte stream

      1) FileInputStream
      2) FileOutputStream

Classes handling Java I/O through character stream

      1) FileReader
      2) FileWriter
      3) BufferedReader
      4) BufferedWriter
      5) PrintWriter

Leave a reply