Java IO——DataInputStream和DataOutputStream

DataInputStream和DataOutputStream是Java提供的数据输入、输出流。

两个类的构造方法如下:

public DataInputStream(InputStream in);
public DataOutputStream(OutputStream out)

从上面的构造方法我们可以看出,创建对象时,需要传入字节流对象,之所以这样是因为DataInputStream和DataOutputStream的实现使用的是装饰模式

装饰模式:https://blog.youkuaiyun.com/b15735105314/article/details/115913647

上一篇我们讲解了FileInputStream和FileOutputStream,这两个流只能对单个字节或者字节数组进行读写操作,无法对其他的数据类型进行直接读写,这就导致我们在读写操作时需要添加数据转换成字节类型的步骤(或者字节类型转换成其他数据类型),然后才能进行读写操作,这种方式不太方便。

所以Java就在字节输入输出流的基础上封装了数据输入输出流DataInputStream和DataOutputStream。

数据输入流的方法如下(输出方法不介绍,反着来就行):

public final int read(byte b[]) throws IOException {}
public final int read(byte b[], int off, int len) throws IOException {}
public final void readFully(byte b[]) throws IOException {}
public final void readFully(byte b[], int off, int len) throws IOException {}
public final int skipBytes(int n) throws IOException {}
public final boolean readBoolean() throws IOException {}
public final byte readByte() throws IOException {}
public final int readUnsignedByte() throws IOException {}
public final short readShort() throws IOException {}
public final int readUnsignedShort() throws IOException {}
public final char readChar() throws IOException {}
public final int readInt() throws IOException {}
public final long readLong() throws IOException {}
public final float readFloat() throws IOException {}
public final double readDouble() throws IOException {}
public final String readUTF() throws IOException {}

上面所有的读方法除了两个方法名是read的方法之外,其他读方法都有EOFExceptionEOFException是在读取过程中到达文件末尾,抛出此异常。

例如readShort()方法如果读取的字节数不满2个字节的话,会抛出EOFException异常。再如一个文件只有5个字节,第一次使用readShort()读取(成功读取),第二次使用readInt()读取,那么读取的时候,流的指针所在位置到文件结尾就只有3个字节,所以使用readInt()的时候,会抛出EOFException异常。

 

示例:

    public static void output() throws IOException {
        FileOutputStream fos = new FileOutputStream("test03.txt");
        DataOutputStream dataOutputStream = new DataOutputStream(fos);


        dataOutputStream.write(5);    //该方法输出参数的低8八位
        dataOutputStream.writeChar('e');
        dataOutputStream.writeFloat(2.12f);
        dataOutputStream.writeInt(20);
        dataOutputStream.writeByte(10);

        fos.flush();
        fos.close();
    }
    public static void input() throws IOException {
        FileInputStream fis = new FileInputStream("test03.txt");
        DataInputStream  dataInputStream = new DataInputStream(fis);
        int result01 = dataInputStream.read();
        System.out.println(result01);

        char result02 = dataInputStream.readChar();
        System.out.println(result02);

        float result03 = dataInputStream.readFloat();
        System.out.println(result03);

        float result04 = dataInputStream.readInt();
        System.out.println(result04);

        byte result05 = dataInputStream.readByte();
        System.out.println(result05);

        dataInputStream.close();

    }

输出结果:

5
e
2.12
20.0
10

 

### Java IO 流知识点教程 #### 了解基本概念流分类 Java 输入/输出(IO)是应用程序与外部环境之间进行数据交换的核心机制之一。java.io包中的类库提供了多种方式来处理这些交互,包括但不限于文件系统、网络连接其他资源的操作[^1]。 #### 字节流与字符流的区别 在 Java 中有两种主要类型的流:字节流用于传输原始二进制数据;而字符流则专门设计用来高效地读写 Unicode 文本信息。对于每种类型都有对应的输入流(Input Stream) 输出流(Output Stream)[^4]。 #### 文件专属流介绍 为了方便对本地磁盘上的文件执行读取或保存操作,Java 提供了几种类别特别针对文件访问优化过的流对象。其中包括`FileInputStream` `FileOutputStream`, 它们允许直接从文件中读取字节或将字节序列化入指定位置; 同样还有面向字符级别的`FileReader` 及其对应写出版本`FileWriter` [^2]. #### 使用转换器实现不同编码间的互转 当需要将一种形式的数据转化为另一种时——比如把字节数组变成字符串或者反之亦然,则可以利用诸如`InputStreamReader` 或者 `OutputStreamWriter`这样的适配器来进行必要的编码解码工作。这使得即使底层物理介质只支持特定格式(如ASCII),也可以轻松处理更复杂的文本表示法[如 UTF-8][^2]. #### 缓冲技术提升性能表现 通过引入额外的一层抽象即所谓的“缓冲”,可以在一定程度上减少频繁调用实际设备驱动所带来的开销并提高整体吞吐量。具体来说就是采用像`BufferedReader`,` BufferedWriter` 这样的高级组件,在内存里预先分配一块区域暂存即将被处理的信息片段直到达到一定规模再统一提交给目标端点[如硬盘扇区],从而获得更好的效率增益. #### 处理结构化的二进制数据 如果要读写带有固定布局定义的复合型记录而非单纯连续排列着简单数值字段的内容的话,那么就应该考虑运用到`DataInputStream`以及它的伙伴`DataOutputStream`.这两个工具能够帮助解析那些按照预设模式组织起来的多部分消息体,并且提供了一系列便捷的方法去获取其中各个组成部分的具体值[例如整数、浮点数等]. 此外还存在特殊用途的对象串行化接口`ObjectInputStream` / ` ObjectOutputStream`,可用于持久化整个复杂对象图谱至永久储存空间内以便日后恢复重建使用状态完全一致的新实例副本[^3]. ```java // 示例代码展示如何反序列化一个包含不同类型成员变量的对象 String filePath = "d:\\IOTest\\data.dat"; try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) { System.out.println(ois.readInt()); System.out.println(ois.readBoolean()); System.out.println(ois.readChar()); System.out.println(ois.readDouble()); System.out.println(ois.readUTF()); Object dog = ois.readObject(); System.out.println("运行类型为:" + dog.getClass().getName()); System.out.println(dog); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值