JavaIO流学习总结-ObjectOutputStream和ObjectInputStream基本操作练习
package io;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
/**
* 修改日期:2020/03/30
* 修改人:牟松
* ObjectOutputStream和ObjectInputStream基本操作练习
* 修改日期:2020/03/30
* 修改人:牟松
* ObjectOutputStream和ObjectInputStream基本操作练习
* 对象的序列化流,作用:把对象转成字节数据的输出到文件中保存,对象的输出过程称为序列化,可实现对象的持久存储。
class Person implements Serializable{ // 实现序列化接口
private static final long serialVersionUID = 1L;
private String name;
private int age;
* 参考连接:https://www.cnblogs.com/niujifei/p/11499074.html
**/class Person implements Serializable{ // 实现序列化接口
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Person() {
}
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
return name;
}
public void setName(String name) {
this.name = name;
}
this.name = name;
}
public int getAge() {
return age;
}
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
this.age = age;
}
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
class Tool1 {
ObjectOutputStream outputstream=null;
ObjectInputStream intputstream=null;
ObjectOutputStream outputstream=null;
ObjectInputStream intputstream=null;
//读文件中内容
public void read() throws IOException
{
ArrayList<Person> list = new ArrayList<Person>();
intputstream=new ObjectInputStream(new FileInputStream("D:\\Ceshi.txt"));
try {
/* for(;;) //针对写入时是List集合
list.addAll((ArrayList<Person>) intputstream.readObject());*/
for(;;) //针对写入时是单个对象
list.add((Person)intputstream.readObject());
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
finally {
intputstream.close();
for(Person person:list){
System.out.println(person.getName());
}
}
}
//往文件中写内容
public void write() throws IOException
{
//1.创建ObjectOutputStream对象,构造方法中传递字节输出流
outputstream=new ObjectOutputStream(new FileOutputStream("D:\\Ceshi.txt"));
//2.使用ObjectOutputStream对象中的方法writeObject,把对象写入到文件中
ArrayList<Person> list = new ArrayList<Person>();
list.add(new Person("牟松",18));
list.add(new Person("张三",18));
//List集合写入
//outputstream.writeObject(list);
//单个对象写入
outputstream.writeObject(new Person("牟松",18));
outputstream.writeObject(new Person("张三",18));
//3.释放资源
outputstream.close();
}
//追加内容
public void appendObj() throws Exception {
//往文件中写内容
public void write() throws IOException
{
//1.创建ObjectOutputStream对象,构造方法中传递字节输出流
outputstream=new ObjectOutputStream(new FileOutputStream("D:\\Ceshi.txt"));
//2.使用ObjectOutputStream对象中的方法writeObject,把对象写入到文件中
ArrayList<Person> list = new ArrayList<Person>();
list.add(new Person("牟松",18));
list.add(new Person("张三",18));
//List集合写入
//outputstream.writeObject(list);
//单个对象写入
outputstream.writeObject(new Person("牟松",18));
outputstream.writeObject(new Person("张三",18));
//3.释放资源
outputstream.close();
}
//追加内容
public void appendObj() throws Exception {
outputstream=new ObjectOutputStream(new FileOutputStream("D:\\Ceshi.txt",true))
{
// 重写writeStreamHeader()方法,空实现
protected void writeStreamHeader(){};
};
// 写入数据
ArrayList<Person> list = new ArrayList<Person>();
list.add(new Person("牟松",18));
list.add(new Person("张三",18));
outputstream.writeObject(list);
outputstream.close();
}
}
{
// 重写writeStreamHeader()方法,空实现
protected void writeStreamHeader(){};
};
// 写入数据
ArrayList<Person> list = new ArrayList<Person>();
list.add(new Person("牟松",18));
list.add(new Person("张三",18));
outputstream.writeObject(list);
outputstream.close();
}
}
public class Ceshi1 {
public static void main(String[] args) throws Exception
{
Tool1 tool=new Tool1();
try {
tool.read();
//tool.write();
//tool.appendObj();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception
{
Tool1 tool=new Tool1();
try {
tool.read();
//tool.write();
//tool.appendObj();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意:
Static 关键字:静态关键字
被static修饰的成员变量不能被序列化的,因为静态的变量不属于某个对象,而是整个类的,所以不需要随着对象的序列化而序列化。序列化的都是对象
transient 关键字:瞬态关键字,被transient修饰成员变量,不能被序列化
序列化一个对象时,要求它的属性要么是基本数据类型,如果是引用数据类型,这个引用数据类型也必须实现Serializable接口
序列化一个数组,要求元素类型实现Serializable接口