Read a txt file in java.

satheesh

New Member
Messages
883
Reaction score
0
Points
0
Read a txt file in java.​

Copy the code below and paste it.

Code:
import java.io.*;


class Showfile {
    public static void main(String arg[])
    throws IOException
        {
       int i;    
       FileInputStream r;
        try {
            r=new FileInputStream("C:/Documents and Settings/Administrator/Satheesh/build/TEST.txt");
            do{
                i=r.read();
                if(i != -1)
                {
                    System.out.print((char)i);
                }
                 
            }while(i!=-1);
        }
        catch(FileNotFoundException e){
              System.out.print("Error:"+e);
        }
       catch(ArrayIndexOutOfBoundsException e)
       {
               System.out.print("Error:"+e);
       }
       
       }
    }

Save as Showfile.java and Compile and Run it.
 
Top