Java中ObjectInputStream类的defaultReadObject()方法用于从此流中读取当前类的非静态和非瞬态字段。
用法:
public void defaultReadObject()
参数:此方法不接受任何参数。
返回值:此方法返回已读取的值。
错误和异常:该函数引发三个异常,如下所述:
- ClassNotFoundException:如果找不到序列化对象的类,则会引发异常。
- IOException:如果发生I /O错误,则会引发异常。
- NotActiveException:如果流当前未读取对象,则抛出异常。
以下示例程序旨在说明上述方法:
程序1:
Java
// Java program to illustrate
// the above method
import java.io.*;
public class GFG {
public static void main(String[] args)
{
try {
// create a new file
// with an ObjectOutputStream
FileOutputStream out
= new FileOutputStream("Shubham.txt");
ObjectOutputStream out1
= new ObjectOutputStream(out);
// write
out1.writeObject(new solve());
// Flushes the stream
out1.flush();
// create an ObjectInputStream
// for the file
ObjectInputStream example
= new ObjectInputStream(
new FileInputStream("Shubham.txt"));
// Read from the stream
solve ans = (solve)example.readObject();
System.out.println(ans.str);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
static class solve implements Serializable {
String str = "Geeksforgeeks";
private void readObject(ObjectInputStream res)
throws IOException,
ClassNotFoundException
{
// By using defaultReadObject() method is
// to read non-static fields of the present
// class from the ObjectInputStream
res.defaultReadObject();
}
}
}
输出:
程序2:
Java
// Java program to illustrate
// the above method
import java.io.*;
public class GFG {
public static void main(String[] args)
{
try {
// create a new file
// with an ObjectOutputStream
FileOutputStream out
= new FileOutputStream("Shubham.txt");
ObjectOutputStream out1
= new ObjectOutputStream(out);
// write
out1.writeObject(new solve());
// Flushes the stream
out1.flush();
// create an ObjectInputStream
// for the file
ObjectInputStream example
= new ObjectInputStream(
new FileInputStream("Shubham.txt"));
// Read from the stream
solve ans = (solve)example.readObject();
// System.out.println(ans.str);
System.out.println(ans.in);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
static class solve implements Serializable {
// String str = "Geeksforgeeks";
Integer in = new Integer(112414);
private void readObject(ObjectInputStream res)
throws IOException,
ClassNotFoundException
{
// By using defaultReadObject() method is
// to read non-static fields of the present
// class from the ObjectInputStream
res.defaultReadObject();
}
}
}
输出:
参考:
https://docs.oracle.com/javase/10/docs/api/java/io/ObjectInputStream.html#defaultReadObject()
相关用法
- Java ObjectInputStream available()用法及代码示例
- Java ObjectInputStream readBoolean()用法及代码示例
- Java ObjectInputStream enableResolveObject()用法及代码示例
- Java ObjectInputStream read()用法及代码示例
- 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 defaultReadObject() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。