Java                                                          Home : www.sharetechnote.com

 

 

 

 

Java - FileReader

 

 

 

FileReader01.java

import java.io.*;

 

public class FileReader01 {

 

    public static void main(String[] args)  {

        

        File file = null;

        FileReader fReader = null;  

        

          

        try {

            fReader = new FileReader("temp.txt");

            int data = fReader.read();

 

            while(data != -1) {

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

                data = fReader.read();

            };

            

        } catch(Exception e) {

            e.printStackTrace();

        }

        

        try {

            fReader.close();

        } catch(Exception e) {

            e.printStackTrace();

        }   

    

    }

 

}

 

contents of temp.txt

Hello World

Hello

World

 

      Output :

       

        Hello World

        Hello

        World

 

 

 

FileReader02.java

import java.io.*;

 

public class FileReader02 {

 

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

        

        File file = null;

        FileReader fReader = null;  

        

          

        fReader = new FileReader("temp.txt");

        int data = fReader.read();

 

        while(data != -1) {

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

            data = fReader.read();

        };

        

        fReader.close();

    

    }

 

}

 

contents of temp.txt

Hello World

Hello

World

 

      Output :

       

        Hello World

        Hello

        World