Java ArrayListSerialise

时间:2022-03-05 06:17:57
import java.io.*;
import java.util.*; //ArrayListSerialise
public class A {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
ObjectOutputStream outStream = new ObjectOutputStream(
new FileOutputStream("personnelList.dat"));
ArrayList<Personnel> staffListOut = new ArrayList<>();
ArrayList<Personnel> staffListIn = new ArrayList<>();
Personnel[] staff = { new Personnel(, "Smith", "John"),
new Personnel(, "Jones", "Sally Ann"),
new Personnel(, "Black", "James Paul") };
for (int i = ; i < staff.length; i++)
staffListOut.add(staff[i]);
outStream.writeObject(staffListOut);
outStream.close();
ObjectInputStream inStream = new ObjectInputStream(new FileInputStream("personnelList.dat"));
int staffCount = ;
try {
staffListIn = (ArrayList<Personnel>) inStream.readObject();
// The compiler will issue a warning for the
// above line, but ignore this!
for (Personnel person : staffListIn) {
staffCount++;
System.out.println("\nStaff member " + staffCount);
System.out.println("Payroll number: " + person.getPayNum());
System.out.println("Surname: " + person.getSurname());
System.out.println("First names: " + person.getFirstNames());
}
System.out.println("\n");
} catch (EOFException eofEx) {
System.out.println("\n\n*** End of fi le ***\n");
inStream.close();
}
}
} class Personnel implements Serializable {
private long payrollNum;
private String surname;
private String firstNames; public Personnel(long payNum, String sName, String fNames) {
payrollNum = payNum;
surname = sName;
firstNames = fNames;
} public long getPayNum() {
return payrollNum;
} public String getSurname() {
return surname;
} public String getFirstNames() {
return firstNames;
} public void setSurname(String sName) {
surname = sName;
}
}