Java code to read the content of a TEXT FILE

Steps
  1. Select Java form the categories and Java Application  from the projects, click Next.
  2. Set the Project name as  readText  and set the Create Main Class, click Finish.
  3. Under the  project "readText", you can see two files, double click the Source Packages,you can see a package by name "readtext " ,double click it you will find the Java Main Class FileReadText.java.
  4. Right click the   package  by name "readtext " and select New -> Other 
  5. From Categories select Java and from File Types select Java Class, click Next
  6. Set the Class Name as "ReadFile.java" and ensure the package name is  "readtext ".
Now open the ReadText.java, remove the existing and paste the following code :


------------------------------------------ReadText.java------------------------------------------------------------

package readtext;
import java.util.Scanner;

/**
 *
 * @author ZISSAN
 */
public class ReadText {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int num=0;
        try{
            System.out.println("enter the text address::");
            Scanner in=new Scanner(System.in);
            String address=in.next();
            //enter the address in this format "C:/songs/test2.txt"
            //"test2.txt" is the text file
            // "C:/songs/" is address in my pc
            ReadFile file=new ReadFile(address);
             //"ReadFile" is the name of the class,"ReadFile()" is the name of the constructor
        //constructor is also a method but its name is same as of the class
        //the address of the text file is given as argument of the constructor
        //"file" is the object
        String[] aryLines=file.OpenFile();
        
        //"OpenFile()" is the method defined in the class ReadFile,
        //encapsulated with "file".
        //"aryLines" is an array, stores the content
        // and gets the "textData" value from
        // "OpenFile()" method.
        int i;
        for(i=0;i<file.readLines();i++)
        //"readline()" is a method defined to determine the lenght of
        //the content in the text file    
        {
        System.out.println(aryLines[i]);//displays the content 
        }
            
        }
        catch(Exception er){
        System.out.println("exception has occured");
        //displays the exception if 
        //any io exception occurs
        //to detect this exception,give 
        //a name of unexisting filename
        //in the address above
        }
        
        
    }
}
-------------------------------------------------end of  ReadText.java---------------------------------------------------
Now open the  ReadFile.java remove the existing and paste the following code :
--------------------------------------------ReadFile.java----------------------------------------------------------

package readtext;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
/**
 *
 * @author ZISSAN
 */
class ReadFile {
    private String path="";
     // variable "path" is declared globally 
    // so that its value can be used by all 
    // methods defined in the class "ReadFile"
    public ReadFile(String file_path){
    path=file_path;
    //"ReadFile()" is the constructor,called in java
    // main class file,"Text.java" and file_path" gets
    // the text file address
    }
    public String[] OpenFile() throws IOException
    //"OpenFile()" opens the text file,reads the
    // content and counts the number of line
    {
    int numberOfLines=readLines();
    String[] textData=new String[numberOfLines];
    int i;
    FileReader fr= new FileReader(path);
    BufferedReader textReader=new BufferedReader(fr);
     for(i=0;i<numberOfLines;i++)
    {
    textData[i]=textReader.readLine();
    }
    textReader.close();
    //"textReader" is the buffer,
    //is closed once the content 
    //is stored in the array "textData[]"
    return textData;
    
    }
    int readLines() throws IOException
    //method readLines counts "\n" in the content
    //and therefore returns the number of line in the 
    //the text file
    {
    FileReader file_to_read=new FileReader(path);
    BufferedReader bf=new BufferedReader(file_to_read);
    String aLine;
    int numberOfLines=0;
    while((aLine=bf.readLine())!=null)
    {
    numberOfLines++;
    }
    bf.close();
    return(numberOfLines);
    }
}
-----------------------------------------------------end of ReadFile.java-------------------------------------------------
-------------------------------------------------------------output------------------------------------------------------------
run:
enter the text address::
C:/songs/test.txt
zissan chowdhury
zissan chowdhury
BUILD SUCCESSFUL (total time: 16 seconds)
----------------------------------------------end of output---------------------------------------------------------

No comments:

Post a Comment