新建Product类,给产品添加属性ID、名称name、产品属性property、价格price,封装这些属性,创建五个对象,将这5个对象序列化,并把序列化后的字节信息输出到D盘名为”序列化.txt”文件中;再将”序列化.txt”文件中对象的字节信息进行反序列化,输出到控制台中。
时间: 2024-02-25 18:54:11 浏览: 26
以下是Java的实现代码:
```java
import java.io.*;
public class Product implements Serializable {
private int ID;
private String name;
private String property;
private double price;
public Product(int ID, String name, String property, double price) {
this.ID = ID;
this.name = name;
this.property = property;
this.price = price;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "ID: " + ID + ", Name: " + name + ", Property: " + property + ", Price: " + price;
}
}
public class Main {
public static void main(String[] args) {
Product product1 = new Product(1, "iPhone", "Smartphone", 999);
Product product2 = new Product(2, "iPad", "Tablet", 499);
Product product3 = new Product(3, "MacBook", "Laptop", 1299);
Product product4 = new Product(4, "iMac", "Desktop", 1499);
Product product5 = new Product(5, "AirPods", "Wireless Earbuds", 199);
try {
FileOutputStream fileOut = new FileOutputStream("D:\\序列化.txt");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(product1);
out.writeObject(product2);
out.writeObject(product3);
out.writeObject(product4);
out.writeObject(product5);
out.close();
fileOut.close();
System.out.println("Serialized data is saved in D:\\序列化.txt");
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream fileIn = new FileInputStream("D:\\序列化.txt");
ObjectInputStream in = new ObjectInputStream(fileIn);
while (true) {
try {
Product product = (Product) in.readObject();
System.out.println(product);
} catch (EOFException e) {
break;
}
}
in.close();
fileIn.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
```
这个程序创建了一个名为Product的类,这个类有4个属性:ID、name、property、price。在初始化函数中,这些属性会被赋予相应的值。程序创建了5个Product对象,并将它们序列化到名为“序列化.txt”的文件中。接下来,程序从文件中反序列化对象,并将它们输出到控制台中。
需要注意的是,为了让对象可以被序列化和反序列化,Product类需要实现Serializable接口。此外,在序列化和反序列化过程中,需要用到ObjectOutputStream和ObjectInputStream类。序列化时,需要先创建一个ObjectOutputStream对象,然后使用它的writeObject()方法将对象写入文件;反序列化时,需要先创建一个ObjectInputStream对象,然后使用它的readObject()方法从文件中读取对象。在读取对象时,需要用try-catch语句捕获EOFException异常,以便在读取完所有对象后退出循环。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)