In this section,you will learn how to sort file data.
In this section,you will learn how to sort file data.By sorting, you can arrange the data into meaningful order and analyze it more effectively. Here we have one file, student.txt, which contains the record of 5 students i.e id, name, marks and grade. The data is not properly arranged in the file. Now, we have to display all the file data in a sorted way according to the id of the student. So for this, we have used Comparable class that compares all the data with respect to id and to sort the data, we have used Arrays.sort() method. This data is then written into another file new.txt.
Here is the student.txt file:
4 A 87 A 5 B 99 A+ 1 C 75 A 2 D 55 C 3 E 68 B |
Here is the code:
import java.io.*; import java.util.*; class ShowData implements Comparable { int id; String name; int marks; String grade; public void setId(int id) { this.id = id; } public int getId() { return id; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setMarks(int marks) { this.marks = marks; } public int getMarks() { return marks; } public void setGrade(String grade) { this.grade = grade; } public String getGrade() { return grade; } public int compareTo(Object Student) throws ClassCastException { if (!(Student instanceof ShowData)) throw new ClassCastException("Error"); int ide = ((ShowData) Student).getId(); return this.id - ide; } } public class SortFileData { SortFileData() { int j = 0; ShowData data[] = new ShowData[5]; try { FileInputStream fstream = new FileInputStream("C:/student.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; ArrayList list = new ArrayList(); while ((strLine = br.readLine()) != null) { list.add(strLine); } Iterator itr; for (itr = list.iterator(); itr.hasNext();) { String str = itr.next().toString(); String[] splitSt = str.split(" "); String id = "", name = "", marks = "", grade = ""; for (int i = 0; i < splitSt.length; i++) { id = splitSt[0]; name = splitSt[1]; marks = splitSt[2]; grade = splitSt[3]; } data[j] = new ShowData(); data[j].setId(Integer.parseInt(id)); data[j].setName(name); data[j].setMarks(Integer.parseInt(marks)); data[j].setGrade(grade); j++; } Arrays.sort(data); File file = new File("C:/new.txt"); FileWriter fw = new FileWriter(file, true); BufferedWriter out = new BufferedWriter(fw); System.out.println("********Sorted by id********"); String strVal = ""; for (int i = 0; i < 5; i++) { ShowData show = data[i]; int no = show.getId(); String name = show.getName(); int marks = show.getMarks(); String grade = show.getGrade(); System.out.println(no + " " + name + " " + marks + " " + grade); String d = no + " " + name + " " + marks + " " + grade; ArrayList al = new ArrayList(); al.add(d + "\n"); Iterator itr1 = al.iterator(); while (itr1.hasNext()) { out.write(itr1.next().toString()); out.newLine(); } } out.close(); } catch (Exception e) { } } public static void main(String[] args) { SortFileData data = new SortFileData(); } }