1. 程式人生 > >裝箱拆箱

裝箱拆箱

print static write otf obj highlight new 文件寫入 tput

步驟 :裝箱(1)先將數據裝箱實體對象.
    (2)多個實體對象在裝箱給實體.
    (3)實體集合在保存到文件
拆箱:(1)先將文件中的數據拆給集合
    (2)集合拆給對象
    (3)集合拆給實體


public class FIle_01 { public static void main (String [] args) throws IOException, IOException{ //數據存入集合 ArrayList <Apple> list = new ArrayList<Apple>(); list.add(new Apple("001",12,30)); list.add(new Apple("002",22,40)); //集合保存到文件 ObjectOutputStream ob = new ObjectOutputStream( new FileOutputStream("D://22.txt")); ob.writeObject(list); //文件寫入集合 ObjectInputStream ib = new ObjectInputStream(new FileInputStream("D://22.txt")); try { List <Apple>obj = (List<Apple>) ib.readObject() ; System.out.println(obj); // 集合寫入對象 for(Apple app:obj){ System.out.println(app); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }

  

裝箱拆箱