FileInputStream Class : Read data from File

File management is very useful part in java programs. In this blog, we will see FileInputStream class. This class uses of read data from any .txt, .bat, .java etc file from folder.

In this program, we will read data from demoTextFile.txt. Without this file we can’t run program. So first create this file near your java file.

Better understanding, we will see video first then move to our program.

 

Video on FileInputStream Class

Program on Read data from file

import java.io.*;

class io5

{

                public static void main(String args[]) throws IOException

                {

                                FileInputStream fin = null;

                                try

                                {

                                                fin = new FileInputStream(“demoTextFile.txt”);                // in folder file

                                                //fin = new FileInputStream(“d:/folder/demoTextFile.txt”);       // in folder file

// How to read data from file

// use read() method

 

                                                int i;

                                                do

                                                {

                                                                i = fin.read();

                                                                System.out.print(i);       

                                                }

                                                while(i!=-1);       // -1 => end of file

                                                System.out.println((char)i);

                                }             

                                catch(Exception ex)

                                {

                                                System.out.println(ex);

                                }                                                                             

                }

}

Output:

Display content from demoTextFile.txt [Check yourself]


[sbscrbr_form]

If you have any query on blog example, you can directly comment us or subscribe our website


Earn Money Online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

782total visits,15visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

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

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image

EmailEmail
PrintPrint