博主18年的互联网软件开发经验,从一名程序员小白逐步成为了一名架构师,我想通过平台将经验分享给大家,因此博主每天会在各个大牛网站点赞量超高的博客等寻找该技术栈的资料结合自己的经验,晚上进行用心精简、整理、总结、定稿,每天都会整理到12点,为了就是能让大家能够真正了解该技术栈的真正原理,最终从程序员成为一名真正的架构师,写的不一定是全站做好的,但是是全站最用心的~。
以后我会推出一些列的文章,每天都会更新,每天进步一点点,发布顺序【java的api基础、应用、实战】->【java开源技术栈及源码分析】->【java开源技术栈整合】->【java低代码开发平台的建设】
关注【架构师成长之道】 输入“架构师视频课程”,即可免费获得全套架构师全套课程
一、java.io
1.45 ObjectInputStream.GetField
ObjectInputStream.GetField
是用于读取序列化对象的字段值的抽象类。它提供了一种访问对象字段的方式,可以用于从序列化流中读取对象的状态。以下是关于 ObjectInputStream.GetField
的介绍:
ObjectInputStream.GetField 介绍:
所有字段:
ObjectInputStream.GetField
没有公共字段。
构造方法:
ObjectInputStream.GetField
是一个抽象类,因此不能实例化。
方法摘要:
-
boolean defaulted(String name, boolean defaultValue)
:返回指定字段是否是默认值。 -
byte get(String name, byte defaultValue)
:返回指定字段的byte
类型值。 -
char get(String name, char defaultValue)
:返回指定字段的char
类型值。 -
double get(String name, double defaultValue)
:返回指定字段的double
类型值。 -
float get(String name, float defaultValue)
:返回指定字段的float
类型值。 -
int get(String name, int defaultValue)
:返回指定字段的int
类型值。 -
long get(String name, long defaultValue)
:返回指定字段的long
类型值。 -
Object get(String name, Object defaultValue)
:返回指定字段的对象值。 -
short get(String name, short defaultValue)
:返回指定字段的short
类型值。 -
boolean get(String name, boolean defaultValue)
:返回指定字段的boolean
类型值。
简单使用例子:
javaCopy code import java.io.*; public class ObjectInputStreamGetFieldExample {static class Person implements Serializable {private static final long serialVersionUID = 1L;private String name;private int age; public Person(String name, int age) {this.name = name;this.age = age;} @Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}} public static void main(String[] args) {String fileName = "person.ser";writeObjectToFile(fileName);readObjectFromFile(fileName);} private static void writeObjectToFile(String fileName) {try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName))) {Person person = new Person("Alice", 30);outputStream.writeObject(person);System.out.println("Object written to file successfully.");} catch (IOException e) {e.printStackTrace();}} private static void readObjectFromFile(String fileName) {try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(fileName))) {Object obj = inputStream.readObject();if (obj instanceof Person) {Person person = (Person) obj;System.out.println("Object read from file: " + person);ObjectInputStream.GetField fields = inputStream.readFields();int age = fields.get("age", 0);String name = (String) fields.get("name", null);System.out.println("Age: " + age);System.out.println("Name: " + name);}} catch (IOException | ClassNotFoundException e) {e.printStackTrace();}} }
在这个例子中,我们定义了一个 Person
类,并将其序列化写入文件。然后使用 ObjectInputStream
从文件中读取对象,并通过 readFields()
方法获取对象的字段值,然后打印出来。
应用场景
ObjectInputStream.GetField
主要用于从序列化流中读取对象的字段值。以下是一些使用 ObjectInputStream.GetField
的应用场景及相应的代码实现:
应用场景 1: 从文件中读取序列化对象并获取字段值
javaCopy code import java.io.*; public class ReadSerializedObjectFromFile {static class Person implements Serializable {private static final long serialVersionUID = 1L;private String name;private int age; public Person(String name, int age) {this.name = name;this.age = age;} @Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}} public static void main(String[] args) {String fileName = "person.ser";readObjectFromFile(fileName);} private static void readObjectFromFile(String fileName) {try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(fileName))) {Object obj = inputStream.readObject();if (obj instanceof Person) {Person person = (Person) obj;System.out.println("Object read from file: " + person);// 从序列化流中获取字段值ObjectInputStream.GetField fields = inputStream.readFields();int age = fields.get("age", 0);String name = (String) fields.get("name", null);System.out.println("Age: " + age);System.out.println("Name: " + name);}} catch (IOException | ClassNotFoundException e) {e.printStackTrace();}} }
应用场景 2: 从网络流中读取序列化对象并获取字段值
javaCopy code import java.io.*; import java.net.*; public class ReadSerializedObjectFromNetwork {static class Person implements Serializable {private static final long serialVersionUID = 1L;private String name;private int age; public Person(String name, int age) {this.name = name;this.age = age;} @Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}} public static void main(String[] args) {try {Socket socket = new Socket("localhost", 8080);ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());Object obj = inputStream.readObject();if (obj instanceof Person) {Person person = (Person) obj;System.out.println("Object read from network: " + person);// 从序列化流中获取字段值ObjectInputStream.GetField fields = inputStream.readFields();int age = fields.get("age", 0);String name = (String) fields.get("name", null);System.out.println("Age: " + age);System.out.println("Name: " + name);}inputStream.close();socket.close();} catch (IOException | ClassNotFoundException e) {e.printStackTrace();}} }
这些例子展示了如何从文件和网络流中读取序列化对象,并使用 ObjectInputStream.GetField
获取字段值。
实战例子
以下是一个实战项目示例,演示了如何使用 ObjectInputStream.GetField
从文件中读取序列化对象并获取字段值:
javaCopy code import java.io.*; public class ObjectInputStreamExample { static class Person implements Serializable {private static final long serialVersionUID = 1L;private String name;private int age; public Person(String name, int age) {this.name = name;this.age = age;} @Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}} public static void main(String[] args) {String fileName = "person.ser"; // 写入对象到文件writeObjectToFile(fileName); // 从文件读取对象并处理readObjectFromFile(fileName);} private static void writeObjectToFile(String fileName) {try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName))) {Person person = new Person("Alice", 30);outputStream.writeObject(person);System.out.println("Object written to file successfully.");} catch (IOException e) {e.printStackTrace();}} private static void readObjectFromFile(String fileName) {try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(fileName))) {Object obj = inputStream.readObject();if (obj instanceof Person) {Person person = (Person) obj;System.out.println("Object read from file: " + person);ObjectInputStream.GetField fields = inputStream.readFields();int age = fields.get("age", 0);String name = (String) fields.get("name", null);System.out.println("Age: " + age);System.out.println("Name: " + name);}} catch (IOException | ClassNotFoundException e) {e.printStackTrace();}} }
在这个示例中,我们首先定义了一个 Person
类,该类实现了 Serializable
接口。然后我们将一个 Person
对象序列化并写入到文件中。接着使用 ObjectInputStream
从文件中读取对象,并通过 ObjectInputStream.GetField
获取对象的字段值,并打印出来。