A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 我是楠楠 于 2018-3-16 14:43 编辑

第2章 对象操作流


1.1 概述
用于从流中读取对象的
ObjectInputStream 称为 反序列化流,利用输入流从文件中读取对象
ObjectOutputStream 称为 序列化,利用输出流向文件中写入对象
特点:用于操作对象。可以将对象写入到文件中,也可以从文件中读取对象。
[AppleScript] 纯文本查看 复制代码
package com.itheima_07;
/*
 * 对象操作流:可以用于读写任意类型的对象
 * ObjectOutputStream
 * writeObject
 * ObjectOutputStream(OutputStream out)
 * ObjectInputStream
 * readObject
 * ObjectInputStream(InputStream in)
 *
 * 注意:
 * 使用对象输出流写出对象,只能使用对象输入流来读取对象
 * 只能将支持 java.io.Serializable 接口的对象写入流中
 *
 */
public class ObjectOutputStreamDemo2 {
public static void main(String[] args)  {
}
 
}
1.2 利用序列化流读写对象
[AppleScript] 纯文本查看 复制代码
package com.itheima_07;
 
import java.io.Serializable;
 
public class Student implements Serializable {
 
/**
 *
 */
String name;
int age;
 
public Student(String name,int age) {
this.name = name;
this.age = age;
}
 
 
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age +"]";
}
}
 
package com.itheima_07;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
 
/*
 * 使用对象输出流和读对象输入流写对象
 * Exception in thread "main" java.io.NotSerializableException: com.itheima_07.Student
 * Serializable:序列号,是一个标识接口,只起标识作用,没有方法
 * 当一个类的对象需要IO流进行读写的时候,这个类必须实现该接口
 *
 * Exception in thread "main" java.io.EOFException:当输入过程中意外到达文件或流的末尾时,抛出此异常。
 *
 */
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException  {
//method();
//创建对象输入流的对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
//读取对象
/*Object obj = ois.readObject();
System.out.println(obj);
Object obj2 = ois.readObject();
System.out.println(obj2);
Object obj3 = ois.readObject();
System.out.println(obj3);*/
try {
while(true) {
Object obj = ois.readObject();
System.out.println(obj);
}
} catch(EOFException e) {
System.out.println("读到了文件的末尾");
}
//释放资源
ois.close();
}
 
private static void method() throws IOException, FileNotFoundException {
//创建对象输出流的对象
//FileOutputStream fos = new FileOutputStream("a.txt");
//ObjectOutputStream oos = new ObjectOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
//创建学生对象
Student s = new Student("zhangsan",18);
Student s2 = new Student("lisi",19);
//写出学生对象
oos.writeObject(s);
oos.writeObject(s2);
//释放资源
oos.close();
}
 
}
1.2.1 案例代码:
[AppleScript] 纯文本查看 复制代码
package com.itheima_02;
 
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
 
/*
 * 使用字符流复制文本文件
 *
 * 数据源        IODemo.java
 * 目的地        d:\\IODemo.java
 
 *
 */
public class FileCopyDemo {
public static void main(String[] args) throws IOException  {
//创建字符输入流对象
FileReader fr = new FileReader("IODemo.java");
//创建字符输出流对象
FileWriter fw = new FileWriter("d:\\IODemo.java");
//一次读写一个字符
/*int ch;
while((ch = fr.read()) != -1) {
fw.write(ch);
fw.flush();
}*/
//一次读写一个字符数组
int len;//用于存储读到的字符个数
char[] chs = new char[1024];
while((len = fr.read(chs)) != -1) {
fw.write(chs,0,len);
fw.flush();
}
//释放资源
fw.close();
fr.close();
}
}

1.3 解决对象输入流读取对象出现异常的问题1.3.1 案例代码七:
[AppleScript] 纯文本查看 复制代码
package com.itheima_07;
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.util.ArrayList;
 
/*
 * 解决对象输入流读取对象出现异常的问题
 *
 */
public class ObjectOutputStreamDemo3 {
public static void main(String[] args) throws IOException, ClassNotFoundException   {
//method();
//创建对象输入流的对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("b.txt"));
//读取数据
Object obj = ois.readObject();
//System.out.println(obj);
//向下转型,获取具体的子类对象
ArrayList<Student> list = (ArrayList<Student>) obj;
for (Student student : list) {
System.out.println(student);
}
//释放资源
ois.close();
}
 
private static void method() throws IOException, FileNotFoundException {
//创建对象输出流的对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("b.txt"));
//创建集合对象
ArrayList<Student> list = new ArrayList<Student>();
//添加学生对象
list.add(new Student("wangwu",30));
list.add(new Student("zhaoliu",28));
//写出集合对象
oos.writeObject(list);
//释放资源
oos.close();
}
}

1.4 解决读写对象版本不一致问题
1.4.1 案例代码八:
[AppleScript] 纯文本查看 复制代码
package com.itheima_07;
 
import java.io.Serializable;
 
public class Student implements Serializable {
 
/**
 *
 */
private static final long serialVersionUID = 6361890890437825953L;
String name;
int age;
String gender;
 
public Student(String name,int age) {
this.name = name;
this.age = age;
}
 
 
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", gender=" + gender + "]";
}
 
}

[AppleScript] 纯文本查看 复制代码
package com.itheima_07;
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
 
/*
 * 解决对实现序列化接口出现的黄色警告问题
 * Exception in thread "main" java.io.InvalidClassException
 * 当 Serialization 运行时检测到某个类具有以下问题之一时,抛出此异常。
该类的序列版本号与从流中读取的类描述符的版本号不匹配
该类包含未知数据类型
该类没有可访问的无参数构造方法
 *
 */
public class ObjectOutputStreamDemo4 {
public static void main(String[] args) throws IOException, ClassNotFoundException  {
//method();
method2();
}
//读取学生对象
private static void method2() throws IOException, FileNotFoundException, ClassNotFoundException {
//创建对象输入流的对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c.txt"));
//读取对象
Object obj = ois.readObject();
System.out.println(obj);
//释放资源
ois.close();
}
//写出学生对象
private static void method() throws IOException, FileNotFoundException {
//创建对象输出流的对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c.txt"));
//创建的学生对象
Student s = new Student("qianqi",28);
//写出学生对象
oos.writeObject(s);
//释放资源
oos.close();
}
 
}
更多
第一天
传智播客·黑马程序员郑州校区地址
河南省郑州市 高新区长椿路11号大学科技园(西区)东门8号楼三层
联系电话 0371-56061160/61/62
来校路线  地铁一号线梧桐街站A口出

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马