数据流用于操作基本数据类型和字符串

写和读必须成对出现,简单说就是必须先写出再读入

DataOutputStream

This class is the superclass of all classes that filter output streams. These streams sit on top of an already existing output stream (the underlying output stream) which it uses as its basic sink of data, but possibly transforming the data along the way or providing additional functionality.

The class FilterOutputStream itself simply overrides all methods of OutputStream with versions that pass all requests to the underlying output stream. Subclasses of FilterOutputStream may further override some of these methods as well as provide additional methods and fields.

继承关系

Constructor Summary
Constructor Description
DataOutputStream(OutputStream out) Creates a new data output stream to write data to the specified underlying output stream.
Method Summary
Method Modifier and Type Description
void flush() Flushes this data output stream.
int size() Returns the current value of the counter written, the number of bytes written to this data output stream so far.
void write(byte[] b, int off, int len) Writes len bytes from the specified byte array starting at offset off to the underlying output stream.
void write(int b) Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.
void writeBoolean(boolean v) Writes a boolean to the underlying output stream as a 1-byte value.
void writeByte(int v) Writes out a byte to the underlying output stream as a 1-byte value.
void writeBytes(String s) Writes out the string to the underlying output stream as a sequence of bytes.
void writeChar(int v) Writes a char to the underlying output stream as a 2-byte value, high byte first.
void writeChars(String s) Writes a string to the underlying output stream as a sequence of characters.
void writeDouble(double v) Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity,
void writeFloat(float v) Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high
void writeInt(int v) Writes an int to the underlying output stream as four bytes, high byte first.
void writeLong(long v) Writes a long to the underlying output stream as eight bytes, high byte first.
void writeShort(int v) Writes a short to the underlying output stream as two bytes, high byte first.
void writeUTF(String str) Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.

DatainputStream

A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.

DataInputStream is not necessarily safe for multithreaded access. Thread safety is optional and is the responsibility of users of methods in this class.

继承关系

Constructor Summary
Constructor Description
DataInputStream(InputStream in)

Creates a DataInputStream that uses the specified underlying InputStream.

Method Summary
Method Modifier and Type Description
int read(byte[] b) Reads some number of bytes from the contained input stream and stores them into the buffer array b.
int read(byte[] b, int off, int len) Reads up to len bytes of data from the contained input stream into an array of bytes.
boolean readBoolean() See the general contract of the readBoolean method of DataInput.
byte readByte() See the general contract of the readByte method of DataInput.
char readChar() See the general contract of the readChar method of DataInput.
double readDouble() See the general contract of the readDouble method of DataInput.
float readFloat() See the general contract of the readFloat method of DataInput.
void readFully(byte[] b) See the general contract of the readFully method of DataInput.
void readFully(byte[] b, int off, int len) See the general contract of the readFully method of DataInput.
int readInt() See the general contract of the readInt method of DataInput.
String readLine() Deprecated.This method does not properly convert bytes to characters.
long readLong() See the general contract of the readLong method of DataInput.
short readShort() See the general contract of the readShort method of DataInput.
int readUnsignedByte() See the general contract of the readUnsignedByte method of DataInput.
int readUnsignedShort() See the general contract of the readUnsignedShort method of DataInput.
String readUTF() See the general contract of the readUTF method of DataInput.
static String readUTF(DataInput in) Reads from the stream in a representation of a Unicode character string encoded in modified UTF-8 format; this string of characters is then returned as a String.
int skipBytes(int n) See the general contract of the skipBytes method of DataInput.

利用数据流对基本类型进行读写

package cn.yzy.io;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;/** DataOutputStream* DataInputStream* 数据流* 1.写出后读取* 2.读取的顺序与写出保持一致*/
public class dataTest {public static void main(String[] args) throws IOException {/*DataOutputStream dos = new DataOutputStream(new ByteArrayOutputStream());* 要使用ByteArrayOutputStream的toArray方法,因此不能使用匿名对象*///写出ByteArrayOutputStream baos = new ByteArrayOutputStream();DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(baos));//操作数据(类型)dos.writeUTF("编码辛酸史");dos.writeInt(18);dos.writeBoolean(false);dos.writeChar('b');dos.flush();byte[] datas = baos.toByteArray();//读取DataInputStream dis = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(datas))); String msgString = dis.readUTF();int integer = dis.readInt();Boolean bool = dis.readBoolean();char c = dis.readChar();System.out.println(bool);}
}

数据流关系图

124.数据流(DataOutputStream、DataInputStream)相关推荐

  1. Java学习成长路径

    JavaSE部分 1 JavaSE体系介绍 JDK安装与环境配置 2 变量 注释,Java数据类型,进位制转换,运算符 3 运算符 分支结构 4 分支结构循环结构 5 循环结构 一维数组 6 二维数组 ...

  2. Java当中的IO流-时间api(下)-上

    Java当中的IO流(下)-上 日期和时间 日期类:java.util.Date 系统时间: long time = System.currentTimeMillis(); public class ...

  3. DataOutputStream 类 和DatainputStream类 的主要方法简单介绍,及代码演示。

    DataOutputStream数据输出流 将java基本数据类型写入数据输出流中.并可以通过数据输入流DataInputStream将数据读入. DataOutputStream类 构造函数: Da ...

  4. .dat文件写入byte类型数组_文件字节流、文件字符流、缓冲字节流、缓冲字符流字节数组流、数据流、转换流、对象流...

    一.实操名称: 描述如下流的基本作用:文件字节流.文件字符流.缓冲字节流.缓冲字符流字节数组流.数据流.转换流.对象流二.描述1.文件字节流:包括:FileInputStream,FileOutput ...

  5. 【Java I/O流】File、字符集、字节流、字符流、缓冲流、数据流、对象流、序列化、try-with-resources语句

    I/O流 File File 常用方法 File 练习:搜索.删除.剪切 字符集(Character Set) 字符编码(Character Encoding) 字符编码比较 乱码 try-with- ...

  6. 【Java-IO】File、搜索删除剪切、字符集、字符编码、字节流、将内存中的数据写入文件、字符流、缓冲流、Scanner、格式化输出、数据流、对象流、序列化与反序列化、Files工具类

    IO 文章目录 IO 简介 File 分隔符.大小写 常用方法 练习:搜索.删除.剪切 字符集(Character Set) 字符编码(Character Encoding) 字符编码比较 乱码 字节 ...

  7. 七万字Java基础知识点总结

    计算机基本知识 一个小常识:java严格区分大小写,但是windows不区分,所以在命令提示符里大小写是一样的 命令行采用的字符集是GBK,识别中文的 程序=算法+数据结构 软件=程序+软件工程 冯· ...

  8. 马士兵java note 5

    如何选择数据结构 Array 读快改慢 Linked改快读慢 Hash两者之间 Map 接口 实现Map接口的类用来存储key-value Map接口的实现类有HashMap和TreeMap(二叉树) ...

  9. Java-Java I/O流解读之Object Serialization and Object Streams

    概述 方法概述 哪些类型的对象有资格进行序列化 ObjectInputStream ObjectOutputStream javaioSerializable Externalizable Inter ...

最新文章

  1. HttpServletResponse 的 sendError( )方法以及常用的HttpServletResponse常量级错误代码
  2. linux安装mysql、卸载mysql、设置mysql
  3. 修改$_env php,Laravel如何友好的修改.env配置文件详解
  4. 【原创】C#中的抽象类(abstract class)和接口(interface)的比较
  5. .NET微服务体系结构中为什么使用Ocelot实现API网关
  6. java socket返回_java中用Socket向ServerSocket发送信息,ServerSocket用接收到的Socket返回一条信息,但是返回时报错......
  7. [转]JAVA 在main中访问内部类、方法等
  8. FS4066耐高压1到4节内置MOS的锂电池充电管理芯片
  9. ipad iphone横屏竖屏
  10. Altium DesignerPCB画3Dbody
  11. 使用pano2vr创建全景图
  12. pms后端商品管理系统介绍
  13. python3基础系列之六【输入输出file方法】
  14. AI学习之路(14): 张量的平方计算
  15. Day036 《电影院售票系统》项目全码
  16. pandas python groupby_python – 如何在Pandas groupby之后获得多个条件操作?
  17. 基于人工势场法的移动机器人路径规划研究(Matlab代码实现)
  18. 使用PHPWord把html转成word文档并支持下载
  19. Saber仿真软件no active design解决方法
  20. cena使用自定义校检器

热门文章

  1. android开发(2) - 九宫格的实现
  2. 2016 MacBook删除系统之后系统恢复
  3. 如何安装centos详细步骤
  4. 融合网络位置服务器,全IP网络融合
  5. PyTorch 框架 训练方法与流程记录
  6. hibernate中联合主键要同时重写hashCode()和equals()方法
  7. ReiserFS文件系统坏块的处理(转)
  8. 论文阅读笔记: Modeling Relational Data with Graph Convolutional Networks
  9. unicodemath 编写word公式
  10. 文献总结---The evolution of tit-for-tat in bacteria via the type VI secretion system----初稿