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


Java 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.resolveObject() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。