serializeread.java

来自「Java the UML Way 书中所有源码」· Java 代码 · 共 49 行

JAVA
49
字号
/*
 * SerializeRead.java   E.L. 2001-08-16
 *
 */
import java.io.*;
class SerializeRead {
  public static void main(String[] args) throws Exception{
    FileInputStream instream = new FileInputStream("apartment1.ser");
    ObjectInputStream in = new ObjectInputStream(instream);
    RenovationProject myApartment = (RenovationProject) in.readObject();
    in.close();

    System.out.println("Apartment data read from the file apartment1.ser:");
    for (int i = 0; i < myApartment.getNoOfSurfaces(); i++) {
      System.out.println(myApartment.getSurface(i));
    }

    for (int i = 0; i < myApartment.getNoOfPaints(); i++) {
      System.out.println(myApartment.getPaint(i));
    }

    System.out.println("Surface and paint data, read from the file apartment2.ser");
    instream = new FileInputStream("apartment2.ser");
    in = new ObjectInputStream(instream);
    try {
      while (true) {  // stops when EOFException is thrown
        java.lang.Object obj = in.readObject();
        System.out.println(obj);
      }
    }
    catch (EOFException e) {
    }
    in.close();
  }
}

/* Output with input made by the SerializeWrite program:
Apartment data read from the file apartment1.ser:
Surface: Wall in children's room, length: 4.0 m, width: 3.0 m
Surface: Ceiling in the corridor, length: 6.0 m, width: 3.0 m
Paint: Extra, no. of coats: 3, no. of sq.m./liter: 10.0, price: $10.0
Paint: Super, no. of coats: 2, no. of sq.m./liter: 12.0, price: $8.0
Surface and paint data, read from the file apartment2.ser
Surface: Wall in children's room, length: 4.0 m, width: 3.0 m
Surface: Ceiling in the corridor, length: 6.0 m, width: 3.0 m
Paint: Extra, no. of coats: 3, no. of sq.m./liter: 10.0, price: $10.0
Paint: Super, no. of coats: 2, no. of sq.m./liter: 12.0, price: $8.0
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?