zoukankan      html  css  js  c++  java
  • Java ArrayListSerialise

    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(123456, "Smith", "John"),
                    new Personnel(234567, "Jones", "Sally Ann"),
                    new Personnel(999999, "Black", "James Paul") };
            for (int i = 0; i < staff.length; i++)
                staffListOut.add(staff[i]);
            outStream.writeObject(staffListOut);
            outStream.close();
            ObjectInputStream inStream = new ObjectInputStream(new FileInputStream("personnelList.dat"));
            int staffCount = 0;
            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("
    Staff 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("
    ");
            } catch (EOFException eofEx) {
                System.out.println("
    
    *** End of fi le ***
    ");
                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;
        }
    }
  • 相关阅读:
    PyQt(Python+Qt)学习随笔:containers容器类部件QStackedWidget重要方法介绍
    什么叫工业4.0,这篇接地气的文章终于讲懂了
    怎样 真正认识一个 人
    华为的绩效管理:减人、增 效、加薪
    羽毛球战术
    魔方教程
    员工培养:事前指导,事后纠正
    一把手瞄准哪里,核心竞争力就在哪里
    海尔的五次战略变革
    如何提高基层员工的执行力
  • 原文地址:https://www.cnblogs.com/rojas/p/5389938.html
Copyright © 2011-2022 走看看