java 为什么序列化多个对象在一个文件里,却反序列化可以读取多个对象 10
importjava.io.Serializable;publicclassProductimplementsSerializable{//成员属性:产品IDid、产品名...
import java.io.Serializable;
public class Product implements Serializable {
//成员属性:产品ID id、产品名称 name、产品属性 categories、产品价格 price
private int id;
private String name;
private String categories;
private double price;
//构造方法
public Product() {
}
public Product(int id,String name,String categories,double price) {
this.setId(id);
this.setName(name);
this.setCategories(categories);
this.setPrice(price);
}
//get、set方法
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getCategories() {
return this.categories;
}
public void setCategories(String categories) {
this.categories = categories;
}
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
//重写toString方法
@Override
public String toString() {
// TODO Auto-generated method stub
String str="产品ID:"+this.getId()+"\n产品名称:"+this.getName()+"\n产品属性:"+this.getCategories()+"\n产品价格:"+this.getPrice()+"元";
return str;
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class TestOne {
public static void main(String[] args) {
// TODO Auto-generated method stub
Product one=new Product(123,"iPhone","telephone",4888);
Product two=new Product(234,"iPad","computer",5088);
Product three=new Product(345,"macbook","computer",10688);
Product four=new Product(256,"iwatch","watch",4799);
try {
FileOutputStream fos=new FileOutputStream("..\\..\\File\\Product");
ObjectOutputStream oos=new ObjectOutputStream(fos);
FileInputStream fis=new FileInputStream("..\\..\\File\\Product");
ObjectInputStream ois=new ObjectInputStream(fis);
//写入文件
oos.writeObject(one);
oos.writeObject(two);
oos.writeObject(three);
oos.writeObject(four);
fos.flush();
oos.flush();
//读取文件
for(int n=0;n<4;n++) {
Product product=(Product)ois.readObject();
System.out.println(product);
System.out.println();
}
fos.close();
oos.close();
fis.close();
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 展开
public class Product implements Serializable {
//成员属性:产品ID id、产品名称 name、产品属性 categories、产品价格 price
private int id;
private String name;
private String categories;
private double price;
//构造方法
public Product() {
}
public Product(int id,String name,String categories,double price) {
this.setId(id);
this.setName(name);
this.setCategories(categories);
this.setPrice(price);
}
//get、set方法
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getCategories() {
return this.categories;
}
public void setCategories(String categories) {
this.categories = categories;
}
public double getPrice() {
return this.price;
}
public void setPrice(double price) {
this.price = price;
}
//重写toString方法
@Override
public String toString() {
// TODO Auto-generated method stub
String str="产品ID:"+this.getId()+"\n产品名称:"+this.getName()+"\n产品属性:"+this.getCategories()+"\n产品价格:"+this.getPrice()+"元";
return str;
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class TestOne {
public static void main(String[] args) {
// TODO Auto-generated method stub
Product one=new Product(123,"iPhone","telephone",4888);
Product two=new Product(234,"iPad","computer",5088);
Product three=new Product(345,"macbook","computer",10688);
Product four=new Product(256,"iwatch","watch",4799);
try {
FileOutputStream fos=new FileOutputStream("..\\..\\File\\Product");
ObjectOutputStream oos=new ObjectOutputStream(fos);
FileInputStream fis=new FileInputStream("..\\..\\File\\Product");
ObjectInputStream ois=new ObjectInputStream(fis);
//写入文件
oos.writeObject(one);
oos.writeObject(two);
oos.writeObject(three);
oos.writeObject(four);
fos.flush();
oos.flush();
//读取文件
for(int n=0;n<4;n++) {
Product product=(Product)ois.readObject();
System.out.println(product);
System.out.println();
}
fos.close();
oos.close();
fis.close();
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 展开
1个回答
2018-08-17 · 知道合伙人互联网行家
关注
展开全部
readObject返回值为Object对象,从表面看我们只能读取一个序列化对象,但是数组也是Object对象,所以我们可以把序列化对象数组读取出来(List等集合框架也是好的选择),这样就可以实现读取多个对象。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Test {
public Test() {
// 创建序列化的对象数组
MyData [] myDatas = new MyData[10];
// 通过循环构造每一个对象
for(int i = 0; i < myDatas.length; i++){
myDatas[i] = new MyData("数据:" + (i + 1));
}
// 对象将要保存的文件
File file = new File("C:/mydata.dat");
// 对象输出流
ObjectOutputStream out = null;
// 对象输入流
ObjectInputStream in = null;
try {
// 将数组对象写入文件
out = new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(myDatas);
out.flush();
out.close();
// 将数组对象从文件中读取
in = new ObjectInputStream(new FileInputStream(file));
MyData [] datas = (MyData[]) in.readObject();
for (MyData myData : datas) {
System.out.println(myData);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Test();
}
}
/**
* 序列化数据
*/
class MyData implements Serializable {
private String text = null;
public MyData(String text) {
this.text = text;
}
public String toString() {
return text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Test {
public Test() {
// 创建序列化的对象数组
MyData [] myDatas = new MyData[10];
// 通过循环构造每一个对象
for(int i = 0; i < myDatas.length; i++){
myDatas[i] = new MyData("数据:" + (i + 1));
}
// 对象将要保存的文件
File file = new File("C:/mydata.dat");
// 对象输出流
ObjectOutputStream out = null;
// 对象输入流
ObjectInputStream in = null;
try {
// 将数组对象写入文件
out = new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(myDatas);
out.flush();
out.close();
// 将数组对象从文件中读取
in = new ObjectInputStream(new FileInputStream(file));
MyData [] datas = (MyData[]) in.readObject();
for (MyData myData : datas) {
System.out.println(myData);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Test();
}
}
/**
* 序列化数据
*/
class MyData implements Serializable {
private String text = null;
public MyData(String text) {
this.text = text;
}
public String toString() {
return text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
追问
我是指为什么我的代码这么写可以读取出来。能不能说下是什么原理
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |