I'm trying to make Students objects once at a time and serialize one at a time to a file, Later I would like to retrieve an object where student first and last name matches.
This is what I have been trying to do
public static void addtoStudents(String First,String Last) {
Student student = new Student(First,Last);
ArrayList<Student> student=new ArrayList<>();
try {
FileOutputStream fop=new FileOutputStream("object.ser");
ObjectOutputStream oos=new ObjectOutputStream(fop);
student.add(student);
oos.writeObject(student);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
And Here is the retrieval code
public static String[] findSomeone(String First,String Last)
{
try {
FileInputStream fis=new FileInputStream("object.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
ArrayList<Student> student=new ArrayList<>();
student=(ArrayList<Student>)ois.readObject();
for(int i=0;i<student.size();i++){
student.get(i).getFirstname();
System.out.println(student.get(i).getFirstname());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
I'm trying to make Students objects once at a time and serialize one at a time to a file, Later I would like to retrieve an object where student first and last name matches.
This is what I have been trying to do
public static void addtoStudents(String First,String Last) {
Student student = new Student(First,Last);
ArrayList<Student> student=new ArrayList<>();
try {
FileOutputStream fop=new FileOutputStream("object.ser");
ObjectOutputStream oos=new ObjectOutputStream(fop);
student.add(student);
oos.writeObject(student);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
And Here is the retrieval code
public static String[] findSomeone(String First,String Last)
{
try {
FileInputStream fis=new FileInputStream("object.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
ArrayList<Student> student=new ArrayList<>();
student=(ArrayList<Student>)ois.readObject();
for(int i=0;i<student.size();i++){
student.get(i).getFirstname();
System.out.println(student.get(i).getFirstname());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.