JavaIO流学习总结-ObjectOutputStream和ObjectInputStream基本操作练习
Posted musong1998
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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基本操作练习
*/
class Person implements Serializable{ // 实现序列化接口
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
* 修改日期:2020/03/30
* 修改人:牟松
* ObjectOutputStream和ObjectInputStream基本操作练习
*/
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;
//读文件中内容
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());
}
}
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 {
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();
}
}
//往文件中写内容
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();
}
}
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();
}
}
}
参考连接:
https://www.cnblogs.com/niujifei/p/11499074.html
以上是关于JavaIO流学习总结-ObjectOutputStream和ObjectInputStream基本操作练习的主要内容,如果未能解决你的问题,请参考以下文章
JavaIO流学习总结-FileReader和FileWriter基本操作练习
JavaIO流学习总结-InputStreamReader和OutputStreamWriter基本操作练习