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;
        }
    }
  • 相关阅读:
    如何使用和关闭onbeforeunload 默认的浏览器弹窗事件
    用js怎么来判断我已点击了窗体中“关闭”按钮?
    js实现时分秒毫秒计时器
    史上最详细的JavaScript事件使用指南
    【JavaScript】图片加载由模糊变清晰 —— 图片优化
    熟悉 hybrid
    深入理解事件委托
    架构师 资料
    常用工具网站集合
    前端路由实现.
  • 原文地址:https://www.cnblogs.com/rojas/p/5389938.html
Copyright © 2011-2022 走看看