Java.io.ObjectInputStream.resolveObject() 方法



描述

java.io.ObjectInputStream.resolveProxyClass(String[] interfaces) 方法返回一個實現代理類描述符中命名的介面的代理類;子類可以實現此方法以從流中讀取自定義資料以及動態代理類的描述符,從而允許它們對介面和代理類使用備用載入機制。

宣告

以下是 java.io.ObjectInputStream.resolveObject() 方法的宣告。

protected Class<?> resolveProxyClass(String[] interfaces)

引數

interfaces − 在代理類描述符中反序列化的介面名稱列表

返回值

此方法返回指定介面的代理類。

異常

  • IOException − 底層 InputStream 丟擲的任何異常。

  • ClassNotFoundException − 如果找不到代理類或任何命名的介面。

示例

以下示例演示了 java.io.ObjectInputStream.resolveProxyClass() 方法的使用。

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.writeUTF(s);
         oout.flush();

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

         // create a list that will be used to resolve proxy class
         String[] list = {Serializable.class.getName()};

         // print the class proxy
         System.out.println("" + ois.resolveProxyClass(list));
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

讓我們編譯並執行以上程式,這將產生以下結果:

class com.sun.proxy.$Proxy0
java_io_objectinputstream.htm
廣告