-1

The data in the flat file looks like this

   START  
    Student.Number = 14  
    Student.Name = JACK  
    Student.Class = 9  
    END


    START  
    Student.Number = 15  
    Student.Name = EMILY  
    Student.Class = 10  
    END


            File myFile = new File("firstfile.txt");   
            FileReader fileReader = new FileReader(myFile);  
            BufferedReader reader= new BufferedReader(fileReader);   

            String line = null; //string that will hold the contents of the file
            while((line=reader.readLine())!=null)  
            {
                String[] token = line.split("START");
                //int number = Integer.parseInt(token[0].substring(14));
            //  token[0] = token[0].substring(14);
            //  String name = token[1].substring(12);
            //  token[1] = token[1].substring(12); 
                //int std = Integer.parseInt(token[2].substring(13));
            //  token[2] = token[2].substring(13);
} 

I want to put the data into a Map and then sort them on the basis of their Student.Number.

Solved: Used the below approach to come up with a solution. However, please let me know if it can be made better.

    File myFile = new File("firstfile.txt"); 
        FileReader fileReader = new FileReader(myFile);
        BufferedReader reader= new BufferedReader(fileReader);



        String line = null; //string that will hold the contents of the file


        while((line=reader.readLine())!=null)
        {
            ///Do something
            if(line.equals("START"))
            {
                System.out.println("Header Present");
            }

            if(line.contains("Student.number="))
            {
                stuNum = line.substring(15);
                System.out.println(stuNum);

            }

            if(line.contains("Student.name="))
            {
                stuName = line.substring(13);
                System.out.println(stuName);

            }

            if(line.contains("Student.class="))
            {
                stuClass = line.substring(14);
                System.out.println(stuClass);

            }


            if(line.equals("END"))
            {
                System.out.println("Trailer Present");
                myList.add(new balak( 

                            Integer.parseInt(stuNum),
                            stuName,
                            Integer.parseInt(stuClass)



                        ));
            }
        }
        reader.close();
    }catch(IOException ie)
    {
        ie.printStackTrace();
    }

2 Answers2

1
public class Student{
    private String name;
    private int number;
    private int class;
public Student(String name, int number, int class){
  this.name = name;
  this.number = number;
  this.class = class;
}
public String getName(){
        return name;
    }
public int getNumber(){
        return number;
    }
public String getClass(){
        return class;
    }

public void setName(String name){
        this.name=name;
    }
public void setNumber(int number){
        this.number=number;
    }
public void setClass(int class){
        this.class=class;
    }
}


ArrayList<Student> studentList= new ArrayList<String>();    
FileReader fr = new FileReader(path);
    BufferedReader textReader = new BufferedReader(fr);
String line;
    while((line = textReader.readLine()) != null){
       if(line.equals("START")){
         Student st = new Student("",0,0);
          student.add(st);
        }else{
          StringTokenizer st = new StringTokenizer(line, "=");
          String title = st.nextElement();
          String data = st.nextElement();
          if(title.equals("Student.Number"))
             student.get(student.size-1).setNumber(data);
          if(title.equals("Student.Class"))
             student.get(student.size-1).setClass(data);
          if(title.equals("Student.Name"))
             student.get(student.size-1).setName(data);

        }
    }
    textReader.close();

then take a look on this.

I hope I solve your problem.

Community
  • 1
  • 1
Emanonk
  • 174
  • 3
  • 16
0

This way you can read the lines:

final List<String> lines = new ArrayList<String>();
final BufferedReader reader = new BufferedReader(new FileReader(path_to_file));

String text = null;
while ((text = reader.readLine()) != null)
    lines.add(text);

Then, you will need to iterate through the lines looking for "START" and "END" keywords.

When you evaluate the lines between the keywords, use trim() to remove the spaces and the split("=") to separate the key from the value. It will return an array of strings. You should use the first string as the key to your map and the second one as the value.

I also suggest that you create a Java Bean, to hold that information. It will be better to sort and manage it than using a list of maps.

In the bean class do your own implementation of the equals method so the Collections.sort() method can be used.

gramos
  • 67
  • 1
  • 11