Details of FileInputStream and FileOutputStream?

java.io.FileInputStream Class:

  • It is a concrete subclass of all Inputstream classes.
  • When we want to open a file in read mode, we must create an object of FileInputStream class by using its constructor.
  • Constructor: FileInputStream (String filename) throws FileNotFoundException

Here String filename represents the name of the file to be opened in read mode.

  • Opening a file in read mode:

FileInputStream fis= new FileInputStream (“abc.txt”);

  • When, we are opening a file, there can be 2 cases:
    • The file may exist in the hard disk.

If “abc.txt” exists in the hard disk then it will be opened successfully in read mode and whose address is available or stored in “fis” (Object of FileInputStream class) and “fis” is pointing to the + first entry of the file “abc.txt”.

    • The file may not exist in the hard disk.

If “abc.txt” file does not exist in hard disk then object “fis” contains null and we get an Exception called FileNotFoundException.

 

java.io.FileOutPutStream class:

  • FileOutputStream class is a concrete subclass of all outputStream classes.
  • FileOutputStream class is used for opening a file in write mode.
  • Opening a file in write mode is nothing but creating an object of FileOutputStream class.
  • FileOutputStream class API:

Constructor:

  • FileOutputStream (String fileName)
  • FileOutputStream (String filename, boolean flag).

 

Example for first constructor:

FileOutPutStream fos = new FileOutPutStream (“abc.txt”);

  • When, we are opening a file, there can be 2 cases:
    • The file is existing in the hard disk and boolean value is false by default.

If “abc.txt” exists in the hard disk then it will be opened successfully in write mode and whose address is available or stored in “fos” (Object of FileOutputStream class) and “fos” is pointing to the first entry of the file “abc.txt”.  When we perform write operation on to the file, the existing data will be replaced with new data.

    • The file is not existing in the hard disk and boolean if false by default.

If “abc.txt” file does not exist in hard disk then the file will be created first if and only if there are sufficient disk space, otherwise it will throw IOException. After creation of the file, it will be opened successfully in write mode and “fos” contains the address of the file and points to the first entry of the file.

Example of second constructor:

FileOutPutStream fos = new FileOutPutStream (“abc.txt”, true/false);

  • When, we are opening a file, there can be 3 cases:
    • The file is existing in the hard disk and boolean value is true.

If “abc.txt” exists in the hard disk then it will be opened successfully in write mode and whose address is available or stored in “fos” (Object of FileOutputStream class) and “fos” is pointing to the next to the end of the file “abc.txt”.  When we perform write operation on to the file, the existing data will remain same and new data will be appended next to the existing data.   

    • The file is existing in the hard disk and boolean value is false.

If “abc.txt” exists in the hard disk then it will be opened successfully in write mode and whose address is available or stored in “fos” (Object of FileOutputStream class) and “fos” is pointing to the first entry of the file “abc.txt”.  When we perform write operation on to the file, the existing data will be replaced with new data.

    • The file not existing in the hard disk and boolean value is true/false.

If “abc.txt” does not exist in the hard disk then it will be created first and then will be opned successfully in write mode and whose address is available or stored in “fos” (Object of FileOutputStream class) and “fos” is pointing to the first entry of the file “abc.txt”.  When we perform write operation on to the file, data will be written simply as it does not have data i.e. newly created file.

Sample Program:

Write a Java program to perform the following:

  • Create a file and write integer from -10 to 10.
  • Read integer from a file which is created at the previous step, by opening it into read mode.

Program to write the data in to a file:

 

import java.io.*;
Class FileOutputStreamDemo
{
		public static void main(String arg[])
		{
			DataInputStream dis =  new DataInputStream(System.in);
			System.out.println(“Enter the file name to be opened in write mode : ”);
			String fileName = dis.readLine();
			FileOutputStream fos= new FileOutputStream (fileName);
			for (int i=-10; i<=10; i++)
			{
				fos.write(i);
}
fos.close();
dis.close();
}
} 

Program to read the data from a file:

import java.io.*;
Class FileInputStreamDemo
{
		public static void main(String arg[])
		{
			DataInputStream dis =  new DataInputStream(System.in);
			System.out.println(“Enter the file name to be opened in read mode : ”);
			String fileName = dis.readLine();
			FileInputStream fis= new FileInputStream (fileName);
			int k=0;
			while(k!=-1)
			{
				K=fis.read();
				System.out.print(k);
}
fis.close();
dis.close();
}
} 

Leave a Reply

Your email address will not be published. Required fields are marked *