Untitled
unknown
plain_text
a year ago
3.4 kB
7
Indexable
Never
package StudentPack; import java.io.*; public class Depersist { public static void main(String[] args) { try { FileInputStream fin = new FileInputStream("f.txt"); ObjectInputStream in = new ObjectInputStream(fin); Student s = (Student)in.readObject(); in.close(); System.out.println(s.name+" "+s.id); }catch(Exception e) { System.out.println(e); } } } package StudentPack; import java.io.Serializable; public class Student implements Serializable { int id; String name; public Student(int id,String name) { this.id=id; this.name=name; } } package StudentPack; import java.io.*; public class Persist { public static void main(String[] args) { try { Student s1 =new Student(211,"ravi"); FileOutputStream fout= new FileOutputStream("f.txt"); ObjectOutputStream out = new ObjectOutputStream(fout); out.writeObject(s1); out.flush(); out.close(); System.out.println("Success"); }catch(Exception e) { System.out.println(e); } } } package L2; import java.io.*; import java.util.*; class Student{ int rollno,year,sem,age; String name,branch; float fees; static String clg; public Student(int rollno,String name,float fees,String branch,int year,int sem,int age) { this.age=age; this.branch=branch; this.fees=fees; this.name=name; this.rollno=rollno; this.sem=sem; this.year=year; clg="PU"; } public String toString() { return rollno+" "+name+" "+fees+" "+branch+" "+year+" "+sem+" "+age+" "+clg+"\n"; } } class AgeComparator implements Comparator{ public int compare(Object o1,Object o2) { Student s1=(Student)o1; Student s2=(Student)o2; if(s1.age==s2.age) return 0; else if(s1.age>s2.age) return 1; else return -1; } } class NameComparator implements Comparator{ public int compare(Object o1, Object o2) { Student s1=(Student)o1; Student s2=(Student)o2; return s1.name.compareTo(s2.name); } } class FeeComparator implements Comparator{ public int compare(Object o1,Object o2) { Student s1=(Student)o1; Student s2=(Student)o2; if(s1.fees==s2.fees) return 0; else if(s1.fees>s2.fees) return 1; else return -1; } } public class Temp { public static void main(String[] args) { ArrayList sl = new ArrayList(); sl.add(new Student(1,"Shiva",10000.00f,"cse",1,1,18)); sl.add(new Student(2,"Venky",15000.00f,"ise",1,2,20)); sl.add(new Student(3,"Jesus",17000.00f,"ece",1,1,19)); sl.add(new Student(3,"Alla",12000.00f,"eee",1,1,19)); sl.add(new Student(3,"Budda",12000.00f,"mec",1,1,21)); System.out.println("Sorting by Name"); System.out.println("_________________"); Collections.sort(sl,new NameComparator()); Iterator itr = sl.iterator(); while(itr.hasNext()) { Student st=(Student)itr.next(); System.out.println(st); } System.out.println("Sorting by Age"); System.out.println("_________________"); Collections.sort(sl,new AgeComparator()); Iterator itr1 = sl.iterator(); while(itr1.hasNext()) { Student st=(Student)itr1.next(); System.out.println(st); } System.out.println("Sorting by Fees"); System.out.println("_________________"); Collections.sort(sl,new FeeComparator()); Iterator itr2 = sl.iterator(); while(itr2.hasNext()) { Student st=(Student)itr2.next(); System.out.println(st); } } }