DataOutputStream 和 ObjectOutputStream的共同点是:

1、写出后读取

2、读取顺序和写出一致

数据流操作:

// 写入
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(baos));
dos.writeUTF("编码辛酸泪");
dos.writeInt(18);
dos.writeBoolean(false);
dos.writeChar('a');
dos.flush();
byte[] datas = baos.toByteArray();
System.out.println(datas.length); // 写出
ByteArrayInputStream bais = new ByteArrayInputStream(datas);
DataInputStream dis = new DataInputStream(new BufferedInputStream(bais));
String msg = dis.readUTF();
int age = dis.readInt();
boolean flag = dis.readBoolean();
char ch = dis.readChar();
System.out.println(ch);

对象流操作:

对象的写入写入又叫序列化和方序列化,需要注意的是,并不是所有的对象都可以序列化,如果要对对象进行序列化,需要实现 Serializable 接口,如果不想让对象中某个属性序列化,可以使用 transient 关键字标识

比如我们自己定义一个类:

class Employee implements Serializable{
private transient String name;
private double salary;
public Employee() {
}
public Employee(String name, double salary) {
super();
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;
} }

序列化到字节数组:

// 写出--> 序列化
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(baos));
// 操作数据类型
oos.writeUTF("编码辛酸泪");
oos.writeInt(18);
oos.writeBoolean(false);
oos.writeChar('a'); // 操作对象
oos.writeObject("谁解其中味");
oos.writeObject(new Date());
oos.writeObject(new Employee("马云", 400));
oos.flush();
byte[] datas = baos.toByteArray();
oos.close(); // 读取--> 反序列化
ByteArrayInputStream bais = new ByteArrayInputStream(datas);
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(bais)); String msg = ois.readUTF();
int age = ois.readInt();
boolean flag = ois.readBoolean();
char ch = ois.readChar();
System.out.println(age); // 对象还原
Object str = ois.readObject();
Object date = ois.readObject();
Object emp = 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(emp instanceof Employee) {
Employee empObj = (Employee) emp;
System.out.println(empObj.getName() + "-->" + empObj.getSalary());
} ois.close();
}

序列化到文件:

// 写出--> 序列化
ObjectOutputStream oos = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream("obj.ser")));
// 操作数据类型
oos.writeUTF("编码辛酸泪");
oos.writeInt(18);
oos.writeBoolean(false);
oos.writeChar('a'); // 操作对象
oos.writeObject("谁解其中味");
oos.writeObject(new Date());
oos.writeObject(new Employee("马云", 400));
oos.flush();
oos.close(); // 读取--> 反序列化
ObjectInputStream ois = new ObjectInputStream(
new BufferedInputStream(new FileInputStream("obj.ser"))); String msg = ois.readUTF();
int age = ois.readInt();
boolean flag = ois.readBoolean();
char ch = ois.readChar();
System.out.println(age); // 对象还原
Object str = ois.readObject();
Object date = ois.readObject();
Object emp = 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(emp instanceof Employee) {
Employee empObj = (Employee) emp;
System.out.println(empObj.getName() + "-->" + empObj.getSalary());
} ois.close();

Java IO 流 -- 数据流和对象流 DataOutputStream ObjectOutputStream的更多相关文章

  1. JAVA基础复习与总结<八> 缓冲流_数据流_对象流_IO总结

    缓冲流.数据流以及对象流 一.缓冲流 缓冲流的概念:在读写的时候,对于单字节的读取会造成硬盘的频繁读写,增加访问次数,降低了读取文件的效率.而引入缓冲流之后,就可以将多个字节写入缓冲区,在缓冲区积累之 ...

  2. Java IO(十一) DataInputStream 和 DataOutputStream

    Java IO(十一) DataInputStream 和 DataOutputStream 一.介绍 DataInputStream 和 DataOutputStream 是数据字节流,分别继承自 ...

  3. 系统学习 Java IO (十二)----数据流和对象流

    目录:系统学习 Java IO---- 目录,概览 DataInputStream/DataOutputStream 允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型. 要想使用 ...

  4. JAVA IO操作:数据操作流:DataOutputStream和DataInputStream

    掌握DataOutputStream和DataInputStream的作用. 可以使用DataOutputStream和DataInputStream写入和读取数据. 在IO包中提供了两个与平台无关的 ...

  5. 一、I/O操作(缓存流,数据流,对象流)

    一.缓存流 以介质是硬盘为例子说明,字节流和字符流的缺点: 每次读写的时候,都会访问硬盘,如果读写频率比较高的时候,性能不佳.为了解决问题,采用缓存流. 缓存流在读取的时候,会一次性读较多的数据到缓存 ...

  6. java.IO输入输出流:过滤流:buffer流和data流

    java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...

  7. Java IO详解(二)------流的分类

    一.根据流向分为输入流和输出流: 注意输入流和输出流是相对于程序而言的. 输出:把程序(内存)中的内容输出到磁盘.光盘等存储设备中      输入:读取外部数据(磁盘.光盘等存储设备的数据)到程序(内 ...

  8. Java IO(五)——字符流进阶及BufferedWriter、BufferedReader

    一.字符流和字节流的区别 拿一下上一篇文章的例子: package com.demo.io; import java.io.File; import java.io.FileReader; impor ...

  9. Java IO(四)——字符流

    一.字符流 字节流提供了处理任何类型输入/输出操作的功能(因为对于计算机而言,一切都是0和1,只需把数据以字节形式表示就够了),但它们不可以直接操作Unicode字符,因为一个Unicode字符占用2 ...

随机推荐

  1. Jmeter4.0接口测试之断言实战(六)

    在接口测试用例中得有断言,没有断言的接口用例是无效的,一个接口的断言有三个层面,一个是HTTP状态码的断言,另外一个是业务状态码的断言,最后是某一接口请求后服务端响应数据的断言.在Jmeter中增加断 ...

  2. Centos 8 上安装 Consul

    /* 1. 下载二进制安装文件 */下载地址:https://www.consul.io/downloads.html /* 2. 解压缩安装包 */unzip consul_1.6.2_linux_ ...

  3. Web Scraper 性能测试 (-_-)

    刚在研究 Python 爬虫的时候,看到了个小白工具,叫 Web Scraper,于是来测试下好不好用. Web Scraper 是什么? 它是一个谷歌浏览器的插件, 用于批量抓取网页信息, 主要特点 ...

  4. YII2自动初始化脚本

    #!/usr/bin/expect spawn ./init expect "Which environment do you want the application to be init ...

  5. 2.用eclipse创建maven Web

    一.其他步骤与上一个博客相同,故不赘述,这里要记得选war→Finish 二.在项目上右键选Properties 三.搜索到Project Facets,把勾取消掉,点Apply 四.重新勾选后出现以 ...

  6. java单元/集成测试中使用Testcontainers

    1.Testcontainers介绍: Testcontainers是一个Java库,它支持JUnit测试,提供公共数据库.SeleniumWeb浏览器或任何可以在Docker容器中运行的轻量级.一次 ...

  7. 使用错误代码对象进行C++错误处理

    原文发表于codeproject,由本人翻译整理分享于此. 前言 我已经使用了本文描述的代码和机制近20年了,到目前为止,我还没有找到更好的方法来处理大型C++项目中的错误.最初的想法是从一篇文章(D ...

  8. markdown 插入图片太大?怎么设定图片大小?

    你一定在插入图片的时候,遇到图片太大,影响观感的问题. Markdown中,图片大小的设定方式有两种 第一种: ![](https://img2018.cnblogs.com/blog/1735896 ...

  9. Vlan间通讯,动态路由

    Vlan间通讯,动态路由 案例1:三层交换vlan间通信 案例2:多交换机vlan间通信 案例3:三层交换配置路由 案例4:RIP动态路由配置 案例5:三层交换配置RIP动态路由 1 案例1:三层交换 ...

  10. adb工作常用命令

    adb devices 查看手机名 adb shell链接手机 dumpsys window windows |grep -i current 打开软件,查看软件入口,和包名,白色为包名,红框为包入口 ...