- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我检查了界面
Serializable
没有函数定义,但当我定义时
private void readObject(ObjectOutputStream oos){
System.out.println("readObject!!");
}
private void writeObject(ObjectOutputStream oos){
System.out.println("writeObject!!");
}
类中的函数,它们在对象被序列化时被调用。
这对我来说很奇怪,如果接口(interface)定义了这两个函数,那么我应该重写它们以确保调用它们。
但是在 Serialized 中,编译器如何生成代码,如果我定义自己的“writeObject”/“readObject”,它们会在序列化时被调用?
我尝试附加
@Override
两个函数之上的注释,编译器报告错误。
那么它到底是如何工作的,你能帮忙解释一下吗?
非常感谢!
最佳答案
java.io.Serialized 是一个函数式接口(interface),因此这意味着它没有定义任何方法。如果您确实想确保没有人会尝试修改您的重写方法,则添加 @Override 注释。 @Override 出现编译器错误的原因是 Serialized 中没有此类方法,但您可以在 ObjectInputStream 和 ObjectOutputStream (分别用作低级类 FileInputStream 和 FileOutputStream)中找到它们。如果你真的想对列表进行序列化,你可以这样做:
package Chaper8.IO;
import java.io.*;
import java.util.*;
public class Serialization_Deserialization {
public static void main(String [] args){
/*
* try-catch with resources, JVM makes sure to close the resources after you've finished using it
* much easier than using finally and getting an exception for each resource closed
*
*/
try(FileOutputStream out = new FileOutputStream("C:\\Users\\Andrei\\Desktop\\Exemple\\worker.txt");
ObjectOutputStream oos = new ObjectOutputStream(out);
FileInputStream in = new FileInputStream("C:\\Users\\Andrei\\Desktop\\Exemple\\worker.txt");
ObjectInputStream ois = new ObjectInputStream(in);){
//instances of the Worker class
Worker w1 = new Worker("Worker1", 123456 , 2000.5);
Worker w2 = new Worker("Worker2", 765436, 1500.15);
Worker w3 = new Worker("Worker3", 364582, 1700.45);
Worker w4 = new Worker("Worker4", 878234, 2100.34);
ArrayList<Worker> list = new ArrayList<>();
//just adding the persons in the list
list.add(w1);
list.add(w2);
list.add(w3);
list.add(w4);
System.out.println("Doing serialization");
oos.writeObject(list);
System.out.println("Doing deserialization");
ois.readObject();
}catch(IOException | ClassNotFoundException e){
e.printStackTrace();
}
}
}
Worker.java
/*
* Worker class, basic type with variables, constructor, and toString() overridden
* Here I have implemented Serializable for the reason that I need to make sure that
* I will serialize the object within the class
*
* Note that I used transient for id. transient is a special keyword which makes sure
* that id will not be serialized, used for security reasons.
*
* serialVersionUID is another variable which is used during deserialization
* to verify that the sender and receiver of a serialized object have loaded
* classes for that object that are compatible with respect to serialization.
* Throws InvalidClassException if the object has a different serialVersionUID
* than that of the corresponding sender's class.
*
*/
import java.io.*;
class Worker implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private transient int id;
private double wage;
public Worker(String name, int id, double wage){
this.name = name;
this.id = id;
this.wage = wage;
}
public String toString(){
return "Person with name " +
name + " and with id " +
id + " has a salary of " + wage + "$";
}
}
关于java Serialized接口(interface)没有功能,为什么会影响 "writeObject"/"readObject",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53434207/
我在学习 Java IO 的基础知识时发现,为了在类中使用自定义 writeObject 函数,必须将其声明为私有(private)。 private void writeObject(ObjectO
通常,当您反序列化一个类时,您必须为其创建一个单独的实例: try (ObjectInputStream objectInputStream = new ObjectInputStream(new F
这是我的第一个问题,因为到目前为止我总是能找到我需要的东西,但这次一些相对基本的东西让我发疯。所以我有这个类(class): public class QImage { protected Strin
只是为了说明我的问题的背景;我正在编写一个客户端服务器应用程序。当客户端最初连接时,服务器访问数据库,并通过在 for 循环中将对象写入对象输出流,将特定表中的所有项目发送到客户端。 问题是,客户端仅
我正在开发一个 UI,它从 Zookeeper 读取序列化对象,反序列化它,然后将其转换为 JSON。由于某种原因,我无法反序列化 MQTopic 对象。但我可以对其他对象执行相同的操作。 这是将 b
我有一个使用泛型和对象序列化的简单服务器。 (T是输入格式,U是输出格式)。仅处理输入的简化版本如下所示: public class Server implements Runnable {
我正在编写一个服务器/客户端程序。客户端将“请求”(这是为此目的而设计的对象)发送到服务器,服务器使用 ObjectInputStream 对其进行解码。所有“Request”对象都属于同一类,只是数
我正在尝试创建一个实时消息应用程序。但是,当尝试从 ObjectInputStream 读取对象时,代码无缘无故地挂起。没有抛出异常。 try { System.out.println("Tr
我正在尝试测试自定义序列化,但未调用 readObject() 方法。尽管我可以调用 writeObject 方法。我们可以重载readObject方法()吗? public class Te
在我必须做的作业中,我必须编写一个学生对象,其中包含姓名、社会安全号码、通过的类(class)数量以及每门类(class)的成绩。我创建了一个 gpa 方法来计算 gpa,并用我自己的方法重写了 wr
我已将对象保存到数组列表并将文件另存为 .dat,但当我想读取对象时,它只显示 null 而不是值。 类 public class Person implements Serializable {
我刚刚开始学习序列化并尝试实现它。我有一个服务器、一个客户端和一个学生类。 服务器创建我的学生类(class)的初始实例。然后客户端连接到服务器并篡改与学生相关的属性,即提高 GPA。 出于某种原因,
我想从文件中读取不同类型的对象到 ArrayList ,它们都是扩展类 Advertising 的类的实例。我正在尝试使用以下代码: ArrayList ads = new ArrayList();
我们正在尝试使用简单的嵌套对象反序列化一个相当大的对象。这通常需要大约 5 - 10 毫秒。然而,最近我们在这次通话期间遇到了高达 3000 毫秒的随机延迟。我可以一遍又一遍地运行请求,并从调用中获得
我想使用ObjectInputStream从文件中读取对象。下面是 readObject 方法的内部结构: public void readObject(ObjectInputStream inbos
当我运行下面的代码时,objIn.readObject();抛出异常: classNotFoundException project.Edge project.Edge 是我尝试从文件中读取的对象。
我在 Android 应用程序中使用 ObjectOutputStream 和 ObjectInputStream 保存和读取保存在文件中的对象时遇到一些问题我有两个类(class)“锻炼”和“锻炼”
似乎是一个非常简单的问题,但我在谷歌上找不到直接答案。 我在两个进程之间通过 UDP 有一个简单的 IPC channel 。每条消息都包装在 header 类中(例如数据驱动)。 例如。 enum
我的应用程序传输 Twitter 数据并将其写入文件。 while(true){ Status status = queue.poll(); if (status ==
我在加载保存文件时遇到问题,它在 2 个 int 变量的 .readObject() 中显示“找不到符号”错误。我正确声明了变量,并且这两个变量都是全局变量。我不知道为什么。我试图寻找答案,所以我最终
我是一名优秀的程序员,十分优秀!