当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ObjectInputStream enableResolveObject()用法及代码示例


Java中ObjectInputStream类的enableResolveObject()方法用于使流能够替换从流读取的对象。启用后,将为每个要反序列化的对象调用resolveObject(java.lang.Object)方法。

用法:

protected boolean enableResolveObject(
                        boolean enable)

参数:此方法仅接受单个参数。

  • enable:此参数设置为true,以允许对每个要反序列化的对象使用resolveObject()。

返回值:在调用此方法之前,此方法将返回先前的设置。

错误和异常:如果存在安全管理器并且其checkPermission方法拒绝使流能够替换从流读取的对象,则该函数将引发SecurityException。



以下示例程序旨在说明上述方法:

程序1:

Java

// Java program to illustrate 
// the above method 
  
import java.io.*; 
  
public class GFG 
    extends ObjectInputStream { 
  
    public GFG(InputStream in) 
        throws IOException 
    { 
        super(in); 
    } 
  
    public static void main(String[] args) 
    { 
        String s = "Geeksforgeeks"; 
        try { 
  
            // create a new file 
            // with an ObjectOutputStream 
            FileOutputStream out 
                = new FileOutputStream("Shubham.txt"); 
  
            ObjectOutputStream out1 
                = new ObjectOutputStream(out); 
  
            // write 
            out1.writeUTF(s); 
  
            // Flushes the stream 
            out1.flush(); 
  
            // create an ObjectInputStream 
            // for the file 
            GFG example 
                = new GFG( 
                    new FileInputStream("Shubham.txt")); 
  
            System.out.println( 
                example 
                    .resolveObject( 
                        example.readUTF())); 
  
            System.out.println( 
                example 
                    .enableResolveObject(true)); 
        } 
  
        catch (Exception ex) { 
            ex.printStackTrace(); 
        } 
    } 
}

输出:

程序2:

Java

// Java program to illustrate 
// the above method 
  
import java.io.*; 
import java.io.InputStream; 
  
public class GFG 
    extends ObjectInputStream { 
  
    public GFG(InputStream in) 
        throws IOException 
    { 
        super(in); 
    } 
  
    public static void main(String[] args) 
        throws IOException 
    { 
  
        int[] array = { 1, 34, 23, 425, 69, 22 }; 
  
        try { 
  
            // create a new file 
            // with an ObjectOutputStream 
            FileOutputStream out 
                = new FileOutputStream("Shubham.txt"); 
  
            DataOutputStream out1 
                = new DataOutputStream(out); 
  
            // write 
            // for each byte in the buffer 
            for (int val:array) { 
  
                // write character to the dos 
                out1.writeInt(val); 
            } 
  
            // Flushes the stream 
            out1.flush(); 
  
            // create an ObjectInputStream 
            // for the file 
            DataInputStream example 
                = new DataInputStream( 
                    new FileInputStream("Shubham.txt")); 
  
            for (int i = 0; i <= array.length; i++) { 
                int c = example.readInt(); 
                System.out.print(c + " "); 
            } 
            System.out.print("\n"); 
        } 
  
        catch (Exception ex) { 
        } 
    } 
}

输出:

参考:

https://docs.oracle.com/javase/10/docs/api/java/io/ObjectInputStream.html#enableResolveObject(boolean)




相关用法


注:本文由纯净天空筛选整理自SHUBHAMSINGH10大神的英文原创作品 ObjectInputStream enableResolveObject() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。