10.2.9 对象流
程序员文章站
2024-03-04 15:32:59
...
数据流操作的是基本数据类型和字符串,对象流操控的除了基本数据类型还包括其他的各种对象,以及自定义对象;
ObjectInputStream && ObjectOutputStream
1 先写出后读取
2 读取的顺序与写出的顺序一致
3 不是所有对象都可以序列化,必须实现serializable接口;
打开API看一下:
ObjectOutputStream:将Java对象的对象和图形写入OutputStream,可以使用ObjectInputStream读取(重构)对象;
下面的代码为具体的应用实例:
package com.sxt.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/**
* 对象流:
* 1 写出后读取
* 2 读取的顺序与写出保持一致
* 3 不是所有的对象都可以序列化Serializable
*
* ObjectOutputStream
* ObjectInputStream
* @author 韩文韬
*
*/
public class ObjectTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//写出--序列化
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(baos));
//操作数据类型+数据
oos.writeUTF("编码辛酸泪");
oos.writeInt(18);
oos.writeBoolean(false);
oos.writeChar('a');
//加入对象
oos.writeObject("谁解其中味");
oos.writeObject(new Date());
Employee emp=new Employee("马云",400);
oos.writeObject(emp);
oos.flush();
byte[] datas=baos.toByteArray();
System.out.println(datas.length);
//读取--反序列化
ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
//顺序与写出一致
String msg=ois.readUTF();
int age=ois.readInt();
boolean flag=ois.readBoolean();
char ch=ois.readChar();
System.out.println(flag);
//对象的数据还原
Object str=ois.readObject();
Object date=ois.readObject();
Object employee=ois.readObject();
if(str instanceof String) {
String strObj=(String)str;
System.out.println(strObj);
}
if(date instanceof Date) {
Date dateObj=(Date)date;
System.out.println(dateObj);
}
if(employee instanceof Employee) {
Employee empObj=(Employee)employee;
System.out.println(empObj.getName()+"-->"+empObj.getSalary());
}
}
}
//javabean 封装数据
class Employee implements java.io.Serializable{
private transient String name; //transient添加透明效果,表示该数据不需要序列化
private double salary;
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
public Employee() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
输出结果为:
143
false
谁解其中味
Fri May 22 15:15:13 CST 2020
null–>400.0
如何写到文件里面去==》go next
package com.sxt.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/**
* 对象流:
* 1 写出后读取
* 2 读取的顺序与写出保持一致
* 3 不是所有的对象都可以序列化Serializable
*
* ObjectOutputStream
* ObjectInputStream
* @author 韩文韬
*
*/
public class ObjectTest02 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//写出--序列化
ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("han.txt")));
//操作数据类型+数据
oos.writeUTF("编码辛酸泪");
oos.writeInt(18);
oos.writeBoolean(false);
oos.writeChar('a');
//加入对象
oos.writeObject("谁解其中味");
oos.writeObject(new Date());
Employee emp=new Employee("马云",400);
oos.writeObject(emp);
oos.flush();
oos.close();
//读取--反序列化
ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream("han.txt")));
//顺序与写出一致
String msg=ois.readUTF();
int age=ois.readInt();
boolean flag=ois.readBoolean();
char ch=ois.readChar();
System.out.println(flag);
//对象的数据还原
Object str=ois.readObject();
Object date=ois.readObject();
Object employee=ois.readObject();
if(str instanceof String) {
String strObj=(String)str;
System.out.println(strObj);
}
if(date instanceof Date) {
Date dateObj=(Date)date;
System.out.println(dateObj);
}
if(employee instanceof Employee) {
Employee empObj=(Employee)employee;
System.out.println(empObj.getName()+"-->"+empObj.getSalary());
}
ois.close();
}
}
//javabean 封装数据
class Employee implements java.io.Serializable{
private transient String name; //transient添加透明效果,表示该数据不需要序列化
private double salary;
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
public Employee() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
由此可以得到han.txt文档中增加了内容;
上一篇: PHP学习笔记——数组的基本概念及分类