对象流
一.ObjectOutputStream
ObjectOutputStream可以将Java对象写入OutputStream。
public static void main(String[] args) throws Exception {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:/student.data"));
Student stu = new Student();
stu.name = "test";
stu.age = 12;
objectOutputStream.writeObject(stu);
objectOutputStream.close();
}
public static class Student implements Serializable {
public int age;
public String name;
}
二.ObjectInputStream
ObjectInputStream性质与ObjectOutputStream一样,只不过它是从InputStream中读取对象。
public static void main(String[] args) throws Exception {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("d:/student.data"));
Student stuRead = (Student) objectInputStream.readObject();
objectInputStream.close();
System.out.println(stuRead.name);
System.out.println(stuRead.age);
}
public static class Student implements Serializable {
public int age;
public String name;
}
三.序列化
如果要使用ObjectInputStream或ObjectOutputStream,就必定会涉及序列化。对一个类进行序列化或反序列化时,这个类就必须实现Serializable接口。序列化类中的serialVersionUID用于确定反序列化对象是否使用相同版本的类进行序列化。
package come.itjsp;
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
*/
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("Jsp");
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; //该数据不需要序列化
private double salary;
public Employee() {
}
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
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;
}
}
打印流
printStream
package come.itjsp;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class PrintTest01 {
public static void main(String[] args) throws FileNotFoundException {
PrintStream ps = System.out;
ps.println("打印流");
ps.println(true);
ps = new PrintStream(new BufferedOutputStream(new FileOutputStream("print.txt")),true);
ps.println("打印流");
ps.println(true);
//ps.flush();
}
}