📜  Java中的 ObjectInputStream enableResolveObject() 方法及示例

📅  最后修改于: 2022-05-13 01:54:59.532000             🧑  作者: Mango

Java中的 ObjectInputStream enableResolveObject() 方法及示例

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

句法:

protected boolean enableResolveObject(
                        boolean enable)

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

  • enable:此参数设置为 true 以启用对正在反序列化的每个对象的 resolveObject()。

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

错误和异常:如果安全管理器存在并且其 checkPermission 方法拒绝启用流来替换从流中读取的对象,则该函数将抛出SecurityException

下面的程序说明了上述方法:

方案一:

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();
        }
    }
}


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) {
        }
    }
}


输出:

方案二:

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 Java)