ObjectInputStream 类的 readUnshared() 方法从 ObjectInputStream 读取 "unshared" 对象。此方法与 readObject 方法相同,不同之处在于它阻止对 readObject 和 readUnshared 的后续调用返回对通过此调用获得的反序列化实例的其他引用。
用法
public Object readUnshared() throws IOException, ClassNotFoundException
参数
没有传递参数。
返回
对反序列化对象的引用
抛出
类未找到异常
流损坏异常
对象流异常
可选数据异常
IOException
例子1
import java.io.*;
import static java.lang.System.out;
public class ObjectInputStreamreadUnsharedExample1 {
public static void main(String[] args) {
try {
FileOutputStream outstream = new FileOutputStream("file1.txt");
ObjectOutputStream objoutstream = new ObjectOutputStream(outstream);
objoutstream.writeObject(new Task());
objoutstream.flush();
ObjectInputStream objinstream = new ObjectInputStream(new FileInputStream("file1.txt"));
Task obj = (Task) objinstream.readUnshared();
System.out.println("" + obj.st);
} catch (Exception ex) {
ex.printStackTrace();
}
}
static class Task implements Serializable {
String st = "Welcome to javaTpoint";
private String readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
ObjectInputStream.GetField g = in.readFields();
return (String) g.get("st", null);
}
}
}
输出:
Welcome to javaTpoint
例子2
import java.io.*;
import java.io.ObjectInputStream;
public class ObjectInputStreamreadUnsharedExample2 {
public static void main(String[] args) {
try {
FileOutputStream outstream = new FileOutputStream("file1.txt");
ObjectOutputStream objoutstream = new ObjectOutputStream(outstream);
FileInputStream instream = new FileInputStream("file2.txt");
ObjectInputStream objinstream = new ObjectInputStream(instream);
String str=objinstream.toString();
objoutstream.writeObject(new task());
objoutstream.writeObject(str);
objoutstream.flush();
ObjectInputStream objinputstram = new ObjectInputStream(new FileInputStream("file1.txt"));
System.out.println("" + (String) objinstream.readObject());
task obj1 = (task) objinputstram.readUnshared();
System.out.println("string in task class :" + obj1.str);
} catch (Exception ex) {
ex.printStackTrace();
}
}
static class task implements Serializable {
String str = "javaTpoint";
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
}
}
}
输出:
[email protected] string that is in task class :javaTpoint
相关用法
- Java ObjectInputStream readUnshared()用法及代码示例
- Java ObjectInputStream readUnsignedByte()用法及代码示例
- Java ObjectInputStream readUnsingedByte()用法及代码示例
- Java ObjectInputStream readUnsignedShort()用法及代码示例
- Java ObjectInputStream readUTF()用法及代码示例
- Java ObjectInputStream readDouble()用法及代码示例
- Java ObjectInputStream readFields()用法及代码示例
- Java ObjectInputStream readFully()用法及代码示例
- Java ObjectInputStream readInt()用法及代码示例
- Java ObjectInputStream read()用法及代码示例
- Java ObjectInputStream readField()用法及代码示例
- Java ObjectInputStream readShort()用法及代码示例
- Java ObjectInputStream readByte()用法及代码示例
- Java ObjectInputStream readLong()用法及代码示例
- Java ObjectInputStream readStreamHeader()用法及代码示例
- Java ObjectInputStream readObjectOverride()用法及代码示例
- Java ObjectInputStream readChar()用法及代码示例
- Java ObjectInputStream readObject()用法及代码示例
- Java ObjectInputStream readLine()用法及代码示例
- Java ObjectInputStream readBoolean()用法及代码示例
注:本文由纯净天空筛选整理自 Java ObjectInputStream readUnshared() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。