Sorting ArrayList studentRecords based on marks Creating Student object for every student record and adding it to ArrayList String studentDetail = currentLine.split(" ") Creating ArrayList to hold Student objects Creating BufferedReader object to read the input text fileīufferedReader reader = new BufferedReader(new FileReader("C:\\input.txt")) Public static void main(String args)throws IOException marksCompare Class to compare the marksĬlass marksCompare implements int compare(Student s1, Student s2) Step 9 : Write each studentRecords into output text file.Ĭlass nameCompare implements int compare(Student s1, Student s2) Step 8 : Create BufferedWriter object to write the records into output text file.īufferedWriter writer = new BufferedWriter(new FileWriter(“Pass the file location here”)) Step 7 : Sort the ArrayList studentRecords using Collections.sort() method by passing either nameCompare or marksCompare object depending upon on which field you want to sort the text file.Ĭollections.sort(studentRecords, new marksCompare()) StudentRecords.add(new Student(name, marks)) Int marks = Integer.valueOf(studentDetail) String studentDetail = currentLine.split(” “) For each student record, create one Student object and add that Student object into studentRecords. Step 6 : Read every student record from input text file. Step 5 : Create an ArrayList to hold the Student objects.ĪrrayList studentRecords = new ArrayList() Step 4 : Create BufferedReader object to read the input text file.īufferedReader reader = new BufferedReader(new FileReader(“Pass The File Location Here”)) Writer = new BufferedWriter(new FileWriter("C:\\output.txt")) Creating BufferedWriter object to write into output file Reading all the lines of input file one by one and adding them into ArrayList Reader = new BufferedReader(new FileReader("C:\\input.txt")) Creating BufferedReader object to read the input file Create an ArrayList object to hold the lines of input file
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |