之前写过DataInputStream和DataOutputStream,使用这两个类可以对java基本数据类型进行序列化和反序列化。

本篇再来两个新东西:ObjectInputStream,ObjectOutputStream,这二位不但能对java基本数据类型进行操作,还可以直接干对象,使用起来也更方便。不过,值得注意的是,被干的对象需要实现Serializable接口。

请看下面两个示例

public class Student implements Serializable {
private int id;
private String name;
private long age;
private boolean gender; public Student(int id, String name, long age, boolean gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public long getAge() {
return age;
} public void setAge(long age) {
this.age = age;
} public boolean isGender() {
return gender;
} public void setGender(boolean gender) {
this.gender = gender;
} @Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
'}';
}
}

示例1:

 @Test
public void testObjectStream() throws Exception {
String fileName = "E:\\360downloads\\wpcache\\srvsetwp\\student2.txt";
//对象输出流
OutputStream outputStream = new FileOutputStream(fileName);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
Student student = new Student(1, "李连杰", 12, true);
objectOutputStream.writeObject(student);
objectOutputStream.close();
outputStream.close(); //对象输入流
FileInputStream fileInputStream = new FileInputStream(fileName);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Student o = (Student) objectInputStream.readObject();
System.out.println(o); //Student{id=1, name='李连杰', age=12, gender=true}
objectInputStream.close();
fileInputStream.close();
}

示例2:

   @Test
public void testObjectStreamPrimary() throws Exception {
String fileName = "E:\\360downloads\\wpcache\\srvsetwp\\student2.txt";
//对象输出流
OutputStream outputStream = new FileOutputStream(fileName);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
Student student = new Student(1, "李连杰", 12, true);
objectOutputStream.writeInt(student.getId());
objectOutputStream.writeUTF(student.getName());
objectOutputStream.writeLong(student.getAge());
objectOutputStream.writeBoolean(student.isGender());
objectOutputStream.close();
outputStream.close(); //对象输入流
FileInputStream fileInputStream = new FileInputStream(fileName);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
int id = objectInputStream.readInt();
String name = objectInputStream.readUTF();
long age = objectInputStream.readLong();
boolean gender = objectInputStream.readBoolean();
student = new Student(id, name, age, gender);
System.out.println(student);
objectInputStream.close();
fileInputStream.close();
}

IO操作之ObjectInputStream与ObjectOutputStream的更多相关文章

  1. Java IO: 序列化与ObjectInputStream、ObjectOutputStream

    作者:Jakob Jenkov  译者: 李璟(jlee381344197@gmail.com) 本小节会简要概括Java IO中的序列化以及涉及到的流,主要包括ObjectInputStream和O ...

  2. Java IO(六) ObjectInputStream 和 ObjectOutputStream

    Java IO(六) ObjectInputStream 和 ObjectOutputStream 一.介绍 对于对象数据的处理,Java IO 提供了 ObjectInputStream 和 Obj ...

  3. java io系列05之 ObjectInputStream 和 ObjectOutputStream

    本章,我们学习ObjectInputStream 和 ObjectOutputStream ObjectInputStream 和 ObjectOutputStream 介绍 ObjectInputS ...

  4. Java之IO(七)ObjectInputStream和ObjectOutputStream

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7003536.html 1.前言 本章介绍Java字节流中重要的成员,对象流ObjectInputStream和O ...

  5. JAVASE02-Unit08: 文本数据IO操作 、 异常处理

    Unit08: 文本数据IO操作 . 异常处理 * java.io.ObjectOutputStream * 对象输出流,作用是进行对象序列化 package day08; import java.i ...

  6. Java 基本IO操作

    1.基本IO操作     有时候我们编写的程序除了自身会定义一些数据信息外,还需要引用外界的数据,或是将自身的数据发送到外界,这时我们需要使用输入与输出. 1)输入与输出       输入:是一个从外 ...

  7. 013-java中的IO操作-InputStream/Reader、OutputStream/Writer

    一.概述 IO流用来处理设备之间的数据传输,上传文件和下载文件,Java对数据的操作是通过流的方式,Java用于操作流的对象都在IO包中. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称 ...

  8. serialVersionUID, ObjectInputStream与ObjectOutputStream类,Serializable接口,serialVersionUID的作用和用法

    ObjectInputStream与ObjectOutputStream类所读写的对象必须实现Serializable接口,对象中的transient和static类型成员变量不会被读取和写入 Ser ...

  9. Java-IO之对象输入流输出流(ObjectInputStream和ObjectOutputStream)

    ObjectInputStream和ObjectOutputStream的作用是对基本数据和对象进行序列化操作支持.创建文件输出流对应的ObjectOutputStream对象,该ObjectOutp ...

随机推荐

  1. apache主要配置详解

    1. # Deny access to the entirety of your server's filesystem. You must # explicitly permit access to ...

  2. L2 Regularization for Neural Nerworks

    L2 Regularization是解决Variance(Overfitting)问题的方案之一,在Neural Network领域里通常还有Drop Out, L1 Regularization等. ...

  3. iframe父页面和子页面高度自适应

    父页HTML: <iframe  id="mainframe" name="mainframe"  style="width:100%;&quo ...

  4. centOS7挂在windows移动硬盘方法

    1,http://www.tuxera.com/community/open-source-ntfs-3g/ 下载ntfs-3g_ntfsprogs-2016.2.22这个压缩包,可用wget和浏览器 ...

  5. SpringMvc.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. 关于plt.imshow()显示彩图问题

    https://blog.csdn.net/cnnmena/article/details/79613531

  7. Little Sub and Mr.Potato's Math Problem (构造法)

    题目传送门Little Sub and Mr.Potato's Math Problem Time Limit: 2 Seconds      Memory Limit: 65536 KB Littl ...

  8. spark连接hive找不到table

    Caused by: org.apache.spark.sql.catalyst.analysis.NoSuchTableException: Table or view 'xxxx' not fou ...

  9. window下 nginx 80端口被占用

    问题:启动nginx没有反应,查看日志提示 bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in ...

  10. C# 字符串的拆分

    string str = "ABCD"; char[] strCharArr = str.ToCharArray(); //结果 //strCharArr[0]='A', //st ...