Solved programming problem

Read Data From File and Print it in Console in Java.

Source code

package inputstream;

import java.io.FileInputStream;

import java.io.IOException;

public class InputStream {

 

    public static void main(String[] args)

    {

       FileInputStream io = null;

     

       try

       {

           io = new FileInputStream("input.txt");

           int c;

         

           ///read char by char

           while((c =io.read())!= -1 )

           {

               ///convert to char and print

               System.out.print((char)c);

           }

           System.out.println("");

         

       }

       catch(IOException e)

       {

           System.out.println("Could not read File");

       }

       finally

       {

           if(io!=null)

           {

               try

               {

                   io.close();

               }

               catch(IOException e1)

               {

                   System.out.println("File Not Closed!!!");

               }

                     

           }

       }

    }

 

}

Output

.txt File

code to haven

Join the discussion

Add your comment