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


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


ObjectInputStream 類的 readObjectOverride() 方法由使用受保護的 no-arg 構造函數構造 ObjectOutputStream 的 ObjectOutputStream 的可信子類調用。它返回一個對象。

用法

protected Object readObjectOverride()throws IOException , ClassNotFoundException

參數

沒有傳遞參數。

返回

從流中讀取的對象。

拋出

類未找到異常

可選數據異常

IOException

例子1

import java.io.*;
public class ObjectInputStreamreadObjectOverridebExample1 extends ObjectInputStream{
   public ObjectInputStreamreadObjectOverridebExample1(InputStream in) throws IOException {
      super(in);
    }
    
   public static void main(String[] args) {
      String s = "JavaTpoint";
      
      try {
         FileOutputStream outstream = new FileOutputStream("file1.txt");
         ObjectOutputStream objoutstream = new ObjectOutputStream(outstream);
         objoutstream.writeObject(s);
         objoutstream.flush();
         ObjectInputStreamreadObjectOverridebExample1 ois = new       ObjectInputStreamreadObjectOverridebExample1(new FileInputStream("file1.txt"));
         System.out.println("" + (String)ois.readObjectOverride());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出:

null

例子2

import java.io.*; 
   class ObjectInputStreamDemo extends ObjectInputStream 
{ 
  
    public ObjectInputStreamDemo(InputStream in) throws IOException 
    { 
        super(in); 
    } 
} 
public class ObjectInputStreamreadObjectOverridebExample2 extends ObjectInputStream
{ 
    public ObjectInputStreamreadObjectOverridebExample2(InputStream in) throws IOException {
      super(in);
    }
    public static void main(String[] args) 
    { 
        
        String str = "JavaTpoint"; 
        try
        { 
         
            FileOutputStream outstream = new FileOutputStream("file1.txt"); 
            ObjectOutputStream objinstream = new ObjectOutputStream(outstream); 
  
            ObjectInputStream InStream  = new ObjectInputStream(new FileInputStream("file1.txt")); 
 
            objinstream.writeObject(new Test()); 
            objinstream.writeUTF(str); 
            objinstream.flush(); 
  
            Test a = (Test) InStream.readObject(); 
            ObjectInputStreamreadObjectOverridebExample2 ois = new ObjectInputStreamreadObjectOverridebExample2(new FileInputStream("file1.txt"));

  
          System.out.println("" + (String)ois.readObjectOverride());
          System.out.println("" + str);
           
        } 
  
        catch (Exception excpt) 
        { 
            System.out.println("Error"); 
            excpt.printStackTrace(); 
        } 
    } 
  
    static class Test implements Serializable
    { 
       static String ss = "Welcome to javaTpoint"; 
        private String readObject(ObjectInputStream in) 
        throws IOException, ClassNotFoundException 
        { 
                       ObjectInputStream.GetField getfield = in.readFields(); 
  
            return (String) getfield.get("ss", null); 
        } 
  
        
    } 
}

輸出:

null
JavaTpoint




相關用法


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