整理 Java I/O (十一):数据流 - DataInputStream/DataOutputStream

DataInputStream 和 DataOutputStream 是用来装饰其他流,它“允许应用程序以与机器无关方式从底层输入流中读取/写出 基本 Java 数据类型”

DataOutputStream

数据输出流允许应用程序以适当的方式将基本的 Java 数据类型写入到输出流中,然后由输出流“持久化”。

构造方法

DataOutputStream(OutputStream out)

创建一个新的数据输出流,将数据写入指定基础输出流。

其他方法

  • flush()

    清空此数据输出流。

  • size()

    返回计数器 written 的当前值,即到目前为止写入此数据输出流的字节数。

  • write(byte[] b, int off, int len)

    将指定 byte 数组中从偏移量 off 开始的 len 个字节写入基础输出流。

  • write(int b)

    将指定字节(参数 b 的八个低位)写入基础输出流。

  • writeBoolean(boolean v)

    将一个 boolean 值以 1-byte 值形式写入基础输出流。

  • writeByte(int v)

    将一个 byte 值以 1-byte 值形式写出到基础输出流中。

  • writeBytes(String s)

    将字符串按字节顺序写出到基础输出流中。

  • writeChar(int v)

    将一个 char 值以 2-byte 值形式写入基础输出流中,先写入高字节。

  • writeChars(String s)

    将字符串按字符顺序写入基础输出流。

  • writeDouble(double v)

    使用 Double 类中的 doubleToLongBits 方法将 double 参数转换为一个 long 值,然后将该 long 值以 8-byte 值形式写入基础输出流中,先写入高字节。

  • writeFloat(float v)

    使用 Float 类中的 floatToIntBits 方法将 float 参数转换为一个 int 值,然后将该 int 值以 4-byte 值形式写入基础输出流中,先写入高字节。

  • writeInt(int v)

    将一个 int 值以 4-byte 值形式写入基础输出流中,先写入高字节。

  • writeLong(long v)

    将一个 long 值以 8-byte 值形式写入基础输出流中,先写入高字节。

  • writeShort(int v)

    将一个 short 值以 2-byte 值形式写入基础输出流中,先写入高字节。

  • writeUTF(String str)

    以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流。

DataInputStream

使用 DataInputStream 来读取之前数据输出流“持久化”的数据。

构造方法

DataInputStream(InputStream in)

使用指定的底层 InputStream 创建一个 DataInputStream。

其他方法

  • read(byte[] b)

    从包含的输入流中读取一定数量的字节,并将它们存储到缓冲区数组 b 中。

  • read(byte[] b, int off, int len)

    从包含的输入流中将最多 len 个字节读入一个 byte 数组中。

  • readBoolean()

    参见 DataInput 的 readBoolean 方法的常规协定。

  • readByte()

    参见 DataInput 的 readByte 方法的常规协定。

  • readChar()

    参见 DataInput 的 readChar 方法的常规协定。

  • readDouble()

    参见 DataInput 的 readDouble 方法的常规协定。

  • readFloat()

    参见 DataInput 的 readFloat 方法的常规协定。

  • readFully(byte[] b)

    参见 DataInput 的 readFully 方法的常规协定。

  • readFully(byte[] b, int off, int len)

    参见 DataInput 的 readFully 方法的常规协定。

  • readInt()

    参见 DataInput 的 readInt 方法的常规协定。

  • readLong()

    参见 DataInput 的 readLong 方法的常规协定。

  • readShort()

    参见 DataInput 的 readShort 方法的常规协定。

  • readUnsignedByte()

    参见 DataInput 的 readUnsignedByte 方法的常规协定。

  • readUnsignedShort()

    参见 DataInput 的 readUnsignedShort 方法的常规协定。

  • readUTF()

    参见 DataInput 的 readUTF 方法的常规协定。

  • readUTF(DataInput in)

    从流 in 中读取用 UTF-8 修改版格式编码的 Unicode 字符格式的字符串;然后以 String 形式返回此字符串。

  • skipBytes(int n)

    参见 DataInput 的 skipBytes 方法的常规协定。

简单示例

public class DataStreamTest {
    private DataInputStream dis;
    private DataOutputStream dos;
    private FilterInputStream fis;

    public static void main(String[] args) {
        new DataStreamTest().testDataOut();
        new DataStreamTest().testDataIn();
    }

    private void testDataOut() {
        try {
            dos = new DataOutputStream(new FileOutputStream(new File(
                    "D:\\DATA.dat")));

            dos.write("abcdefg".getBytes(), 0, 5);
            dos.write(102);
            dos.writeBoolean(true);
            dos.writeByte(103);
            dos.writeBytes("hij");
            dos.writeChar(107);
            dos.writeChars("lmno");
            dos.writeDouble(100.0);
            dos.writeFloat(200);
            dos.writeInt(300);
            dos.writeLong(400);
            dos.writeShort(500);
            dos.writeUTF("pqrst");

            System.out.println(dos.size());
            dos.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (dos != null) {
                    dos.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    private void testDataIn() {
        try {
            dis = new DataInputStream(new FileInputStream(new File(
                    "D:\\DATA.dat")));

            for (int i = 0; i < 6; i++) {
                System.out.print((char) dis.read());
            }
            System.out.println("\n" + dis.readBoolean());
            System.out.println((char) dis.readByte());
            System.out.println((char) dis.readByte());
            System.out.println((char) dis.readByte());
            System.out.println((char) dis.readByte());
            System.out.println(dis.readChar());
            System.out.println(dis.readChar()); // 懒得循环了,直接读吧
            System.out.println(dis.readChar());
            System.out.println(dis.readChar());
            System.out.println(dis.readChar());
            System.out.println(dis.readDouble());
            System.out.println(dis.readFloat());
            System.out.println(dis.readInt());
            System.out.println(dis.readLong());
            System.out.println(dis.readShort());
            System.out.println(dis.readUTF());

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
Copyright© 2020-2022 li-xyz 冀ICP备2022001112号-1