當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.io.ObjectInputStream readObjectOverride()用法及代碼示例


描述

這個java.io.ObjectInputStream.readObjectOverride()方法由使用受保護的 no-arg 構造函數構造 ObjectOutputStream 的 ObjectOutputStream 的可信子類調用。子類應提供帶有修飾符 "final" 的覆蓋方法。

聲明

以下是聲明java.io.ObjectInputStream.readObjectOverride()方法。

protected Object readObjectOverride()

參數

NA

返回值

此方法返回從流中讀取的對象。

異常

  • ClassNotFoundException− 找不到序列化對象的類。

  • OptionalDataException- 在流中找到原始數據而不是對象。

  • IOException− 如果從底層流讀取時發生 I/O 錯誤

示例

下麵的例子展示了使用java.io.ObjectInputStream.readObjectOverride()方法。

package com.tutorialspoint;

import java.io.*;

public class ObjectInputStreamDemo extends ObjectInputStream{

   public ObjectInputStreamDemo(InputStream in) throws IOException {
      super(in);
    }
    
   public static void main(String[] args) {
      String s = "Hello World";
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeObject(s);
         oout.flush();

         // create an ObjectInputStream for the file we created before
         ObjectInputStreamDemo ois = new ObjectInputStreamDemo(new FileInputStream("test.txt"));

         // read and print an object and cast it as string
         System.out.println("" + (String)ois.readObjectOverride());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果 -

null

相關用法


注:本文由純淨天空篩選整理自 Java.io.ObjectInputStream readObjectOverride() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。