1.Reading the data from a file:

  • FileInputStream class is used to read data from a file.
  • It is two step process.
  1. Create  FileInputStream class object by using its available constructors.
  2. Then call read() method on this object till control reach end of file.
  • Means read() method must be called in loop, because it returns only one byte at a time .
  • Hence it must be called in loop for each byte available in the file till it returns "-1".
  • It returns  -1 if control reached end of the file.
FileInputStream class has below constructors to create its object:

  1.  public FileInputStrean(String name) throws FileNotFoundException
  2.  public FileInputStream(File file)  throws FileNotFoundException
  3.  public FileInputStream(FileDescriptor fd)

Rule:

  • To Create FileInputStream class object the file must be with the passed argument name . else this constructor throws java.io.FileNotFoundException.
  • FileInputStream  class constructor throws FileNotFoundException in below situations.
  • If the file is not existed with the passed name.
  • Passed file name is a directory rather than is a regular file.
  • If file does not have reading permissions.

 Java Program to read data from a file:

  1.  Create a file with name test.txt. with text "hello world" this is for example you can write any text.
  2. Creating stream object connecting to test.txt file.
    FileInputStream fis= new FileInputStream("test.txt");
  3. Reading all bytes from the file till control reaches end of file.
    int i=fis.read();
  4. Printing returned character.
    System.out.println(i);

Program #1: 

  1. package com.instanceofjava;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6.  
  7. public class FISDemo {
  8.  
  9.  public static void main(String[] args) throws FileNotFoundException, IOException
  10. {
  11.  
  12.       FileInputStream fis= new FileInputStream("E:\\test.txt");
  13.  
  14.         int i;
  15.         while((i=fis.read())!=-1){
  16.            System.out.println((char)i);
  17.         }
  18.  
  19.     }
  20. }

Output:

  1. h
  2. e
  3. l
  4. l
  5. o
  6.  
  7. w
  8. o
  9. r
  10. l
  11. d

Program #2: Using read() method

  1. package com.instanceofjava;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6.  
  7. public class FISDemo {
  8.  
  9.  public static void main(String[] args) throws FileNotFoundException, IOException
  10. {
  11.  
  12.       FileInputStream fis= new FileInputStream("E:\\test.txt");
  13.  
  14.        byte[] b=new byte[fin.available()];
  15.  
  16.        // Read data into the array
  17.         fin.read(b);
  18.         for (int i = 0; i < b.length; i++) {
  19.  
  20.             System.out.println((char)b[i]);
  21.  
  22.         }
  23.  
  24.     }
  25. }

Output:

  1. h
  2. e
  3. l
  4. l
  5. o
  6.  
  7. w
  8. o
  9. r
  10. l
  11. d

Program #3: Using read(destination, start-index, last-index) method

  1. package com.instanceofjava;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6.  
  7. public class FISDemo {
  8.  
  9.  public static void main(String[] args) throws FileNotFoundException, IOException
  10. {
  11.  
  12.       FileInputStream fis= new FileInputStream("E:\\test.txt");
  13.  
  14.       byte b1[]=new byte[fin.available()];
  15.       fin.read(b1,0,b1.length);
  16.  
  17.                     for(int i=0;i<b1.length;i++)
  18.                     {
  19.                     System.out.print((char)b1[i]);
  20.                     }
  21.  
  22.     }
  23. }



Output:

  1. hello world

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu