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();
}