Skip to main content

Java read data from file into array of classes

We are often required to read a text file and store it's data into array of  a class. 
Let's take an example where we have a text file movie.txt containing movie name, release date, production cost and profit.

movie.txt looks something like this-
1. Create a new project in eclipse.
2. Create a new class as Movie.java and declare class members, constructor and getter, setter methods.
public class Movie {

        private String name,date,cost,profit;
        
        public Movie(String name, String date, String cost, String profit) {
                super();
                this.name = name;
                this.date = date;
                this.cost = cost;
                this.profit = profit;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public String getDate() {
                return date;
        }
        public void setDate(String date) {
                this.date = date;
        }
        public String getCost() {
                return cost;
        }
        public void setCost(String cost) {
                this.cost = cost;
        }
        public String getProfit() {
                return profit;
        }
        public void setProfit(String profit) {
                this.profit = profit;
        }
}
3. Create a new java class as ReadFromFile.java and add main method in it and type the following code-
import java.io.*;
import java.io.FileReader;
public class ReadFromFile {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                try {
                        
                        //Read File
                        FileReader file=new FileReader("movie.txt");
                        BufferedReader bufferedReader=new BufferedReader(file); 
                        
                        //Declare an array of movie class
                        Movie[] movieArray=new Movie[100];
                        String line="",name="",date="",cost="",profit="";
                        int index=0;
                        while((line=bufferedReader.readLine())!=null){
                                
                                //Split lines when white space is more than or equal to 2
                                String[] tokens=line.split("(\\s){2,}");
                                date=tokens[0];
                                name=tokens[1];
                                cost=tokens[2];
                                profit=tokens[3];                       
                                movieArray[index]=new Movie(name,date,cost,profit);
                                index++;
                        }
                        int noOfLines=index;
                        for(int i=0;i<noOfLines;i++){
                                String myName=movieArray[i].getName();
                                String myDate=movieArray[i].getDate();
                                String myCost=movieArray[i].getCost();
                                String myProfit=movieArray[i].getProfit();
                                if(myName.length()>20){
                                        System.out.println(myName+"\t\t\t"+myDate+"\t\t\t"+myCost+"\t\t\t"+myProfit);
                                }else{
                                        System.out.println(myName+"\t\t\t\t"+myDate+"\t\t\t\t"+myCost+"\t\t\t\t"+myProfit);
                                }
                        }               
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
}
4. Run the project.

Comments

Popular Posts