⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serializeread.java

📁 Java the UML Way 书中所有源码
💻 JAVA
字号:
/*
 * 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -