Home
›
Reading data from file using FIleInputStream
Posted by: InstanceOfJava
Posted date:
Feb 11, 2015
/
1.Reading the data from a file:
- FileInputStream class is used to read data from a file.
- It is two step process.
- Create FileInputStream class object by using its available constructors.
- 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:
- public FileInputStrean(String name) throws FileNotFoundException
- public FileInputStream(File file) throws FileNotFoundException
- 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:
- Create a file with name test.txt. with text "hello world" this is for example you can write any text.
- Creating stream object connecting to test.txt file.
FileInputStream fis= new FileInputStream("test.txt");
- Reading all bytes from the file till control reaches end of file.
int i=fis.read();
- Printing returned character.
System.out.println(i);
Program #1:
- package com.instanceofjava;
-
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
-
- public class FISDemo {
-
- public static void main(String[] args) throws FileNotFoundException, IOException
- {
-
- FileInputStream fis= new FileInputStream("E:\\test.txt");
-
- int i;
- while((i=fis.read())!=-1){
- System.out.println((char)i);
- }
-
- }
- }
Output:
Program #2: Using read() method
- package com.instanceofjava;
-
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
-
- public class FISDemo {
-
- public static void main(String[] args) throws FileNotFoundException, IOException
- {
-
- FileInputStream fis= new FileInputStream("E:\\test.txt");
-
- byte[] b=new byte[fin.available()];
-
- // Read data into the array
- fin.read(b);
- for (int i = 0; i < b.length; i++) {
-
- System.out.println((char)b[i]);
-
- }
-
- }
- }
Output:
Program #3: Using read(destination, start-index, last-index) method
- package com.instanceofjava;
-
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
-
- public class FISDemo {
-
- public static void main(String[] args) throws FileNotFoundException, IOException
- {
-
- FileInputStream fis= new FileInputStream("E:\\test.txt");
-
- byte b1[]=new byte[fin.available()];
- fin.read(b1,0,b1.length);
-
- for(int i=0;i<b1.length;i++)
- {
- System.out.print((char)b1[i]);
- }
-
- }
- }
Output:
No comments