The Artima Developer Community
Sponsored Link

Java Buzz Forum
Reading data from file using FIleInputStream

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Reading data from file using FIleInputStream Posted: Feb 11, 2015 6:42 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Reading data from file using FIleInputStream
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

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.             System.out.println((char)b[i]);
  20.         }
  21.  
  22.     }
  23. }

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

Read: Reading data from file using FIleInputStream

Topic: Top 10 Easy Performance Optimisations in Java Previous Topic   Next Topic Topic: Java EE 7 and WildFly on Kubernetes using Vagrant

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use