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) {
}
}
}
輸出:
參考:
相關用法
- Java ObjectInputStream available()用法及代碼示例
- Java ObjectInputStream readBoolean()用法及代碼示例
- Java ObjectInputStream read()用法及代碼示例
- Java ObjectInputStream defaultReadObject()用法及代碼示例
- Java ObjectInputStream readByte()用法及代碼示例
- Java ObjectInputStream close()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Clock withZone()用法及代碼示例
- Java Clock tickMinutes()用法及代碼示例
注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 ObjectInputStream enableResolveObject() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。