Wednesday, August 24, 2011

Reading Text File in JAVA

One of the most important feature of any programming language is that is should provide an extensive set of Input/Output methods that fit in different scenarios according to programmers need and they should also be simple and powerful as much as they can. JAVA, a powerful and wonderful language is no exception, it provide a rich set of Input/Output operation. In this tutorial we will look some of the ways to provided by JAVA to read a simple Text File. We present only three ways to read a simple text file,  there are other methods too to achieve the same goal. Now let the code talk ...

Reading using BufferedReader

    private void readFileUsingBufferedReader(String filename){
        try{

            FileReader fileReader = new FileReader(filename);
            BufferedReader bufferReader = new BufferedReader(fileReader);
            
            String fileContents=null;

            while((fileContents = bufferReader.readLine()) != null){

                // Print to console line by line
                System.out.println(fileContents);
            }
            bufferReader.close();
        }
        catch(Exception ex){
            System.out.println(ex);
        }
    }

Reading using Scanner

    private void readFileUsingScanner(String filename){
        try{
            File file = new File(filename);

            Scanner scanner = new Scanner(file);
            
            // Scanner split content of file
            // in tokens by default space
            // is the delimiter.

            scanner.useDelimiter(System.getProperty("line.separator"));
            
            // line seperator for windows is \n\r
            // and for linux is \n
            // to make is cross paltform we use System.getProperty method

            while (scanner.hasNext()) {

                // Print to console line by line

                System.out.println(scanner.next());
            }
            scanner.close();
        }
        catch(Exception ex){
            System.out.println(ex);
        }
    }

Reading using FileInputStream

    private void readfileUsingFileInputStream(String filename){
        try{

            FileInputStream fileInputStream = new FileInputStream(filename);
            int k;
            while((k=fileInputStream.read())!=-1){

                // input stream read data byte by byte
                // so we have to explicitly typecast
                // it into char to reval it corresponding
                // charater.

                System.out.print((char)k);
            }
            fileInputStream.close();
        }
        catch(Exception ex){
            System.out.println(ex);
        }
    }
A sample usage of all the above methods...
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.util.Scanner;

/**
 *
 * @author Originative
 */
public class TextFileReader {

    public static void main(String [] nix){

        String TEXT_FILE_TO_BE_READ = "c:\\myTextFile.txt";
        TextFileReader textFileReader=new TextFileReader();

        System.out.println("\n=-=-=-=\n Reading File via BufferedReader \n=-=-=-=\n");
        textFileReader.readFileUsingBufferedReader(TEXT_FILE_TO_BE_READ);

        System.out.println("\n=-=-=-=\n Reading File via Scanner \n=-=-=-=\n");
        textFileReader.readFileUsingScanner(TEXT_FILE_TO_BE_READ);

        System.out.println("\n=-=-=-=\n Reading File via FileInputStream \n=-=-=-=\n");
        textFileReader.readfileUsingFileInputStream(TEXT_FILE_TO_BE_READ);
    }
   // paste all methods here :)
}

Sample Output

=-=-=-=
 Reading File via BufferedReader 
=-=-=-=

Somthing that is going to read by java

code by

Originative for www.tutorialjinni.com

=-=-=-=
 Reading File via Scanner 
=-=-=-=

Somthing that is going to read by java

code by

Originative for www.tutorialjinni.com

=-=-=-=
 Reading File via FileInputStream 
=-=-=-=

Somthing that is going to read by java

code by

Originative for www.tutorialjinni.com

0 comments:

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems