No way to find EOF, Simply store object in a List (e.g. ArrayList, HashMap etc.) then serialize it and write into a file like below..
public class User implements Serializable{
String name;
String pass;
public User(String name, String pass) {
this.name = name;
this.pass = pass;
}
public String getName() {
return name;
}
public String getPass() {
return pass;
}
public void display(){
System.out.println(name+":"+pass);
}
}
public class RecordReader {
public static void main(String as[]) throws Exception{
ObjectInputStream input=new ObjectInputStream(new FileInputStream("e:/u.txt"));
ArrayList u=(ArrayList)input.readObject();
Iterator i=u.iterator();
System.out.println("Records are: ");
while(i.hasNext()){
User u1=(User)i.next();
u1.display();
}
}
}
public class RecordSaver {
public static void main(String as[]) throws Exception{
ArrayList list=new ArrayList();
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("e:/u.txt"));
list.add(new User("raj","kumar"));
list.add(new User("raju","jha"));
out.writeObject(list);
out.close();
System.out.println("Record save successfully");
}
}
No comments:
Post a Comment