Java基础-IO操作-数据流


1. DataOutputStream(字节数据输出流)

允许把数据和其类型一并写出去。

构造器说明
public DataOutputStream(OutputStream outputStream)创建新数据输出流包装基础的字节输出流
方法说明
public final void writeByte(int v) throws IOException将byte类型的数据写入基础的字节输出流
public final void writeInt(int v) throws IOException将int类型的数据写入基础的字节输出流
public final void writeDouble(Double v) throws IOException将Double类型的数据写入基础的字节输出流
public final void writeUTF(String str) throws IOException将字符串数据以UTF-8编码成字节写入基础的字节输出流
public void write(int/byte[]/byte[]的一部分)支持写字节数据出去
public final void writeBoolean(boolean v) throws IOException将boolean类型的数据写入基础的字节输出流
public class DataOutputStreamTest {
    private static final String SAVE_PATH = "D:\\repo\\javasepro\\helloworld-app\\src\\com\\ming\\File_\\static\\DataOutputStreamTest.txt";

    public static void main(String[] args) {
        // 目标:掌握字节数据输出流
        // 1. public DataOutputStream(OutputStream outputStream)    创建新数据输出流包装基础的字节输出流

        // 1. public final void writeByte(int v) throws IOException 将byte类型的数据写入基础的字节输出流
        // 2. public final void writeInt(int v) throws IOException  将int类型的数据写入基础的字节输出流
        // 3. public final void writeDouble(Double v) throws IOException    将Double类型的数据写入基础的字节输出流
        // 4. public final void writeBoolean(boolean v) throws IOException    将boolean类型的数据写入基础的字节输出流
        // 5. public final void writeUTF(String str) throws IOException 将字符串数据以UTF-8编码成字节写入基础的字节输出流
        // 6. public void write(int/byte[]/byte[]的一部分)  支持写字节数据出去


        try (
                // 1. 创建一个数据输出流包装低级的字节输出流
                DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(SAVE_PATH));
        ) {
            // 1. public final void writeByte(int v) throws IOException 将byte类型的数据写入基础的字节输出流
            dataOutputStream.writeByte(97);
            // 2. public final void writeInt(int v) throws IOException  将int类型的数据写入基础的字节输出流
            dataOutputStream.writeInt(97);
            // 3. public final void writeDouble(Double v) throws IOException    将Double类型的数据写入基础的字节输出流
            dataOutputStream.writeDouble(97.5);
            // 4. public final void writeBoolean(boolean v) throws IOException    将boolean类型的数据写入基础的字节输出流
            dataOutputStream.writeBoolean(true);
            // 5. public final void writeUTF(String str) throws IOException 将字符串数据以UTF-8编码成字节写入基础的字节输出流
            dataOutputStream.writeUTF("床前明月光, 疑是地上霜");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2. DataInputStream(字符数据输入流)

用于读取数据输出流写出的数据
注意:读取数据的顺序要跟写入数据的顺序一致,否则会报错!!!

构造器说明
public DataInputStream(InputStream inputStream)创建新数据输出流包装基础的字节输入流
方法说明
public final void readByte() throws IOException读取字节数据返回
public final void readInt() throws IOException读取int数据返回
public final void readDouble() throws IOException读取Double数据返回
public final void readBoolean() throws IOException读取Boolean数据返回
public final void readUTF(String str) throws IOException读取字符串(UTF-8)数据返回
public void read(byte[])支持读取字节数据出去
public class DataInputStreamTest {
    private static final String SAVE_PATH = "D:\\repo\\javasepro\\helloworld-app\\src\\com\\ming\\File_\\static\\DataOutputStreamTest.txt";

    public static void main(String[] args) {
        // 目标:掌握字节数据输入流
        // 1. public DataInputStream(InputStream inputStream)    创建新数据输出流包装基础的字节输入流

        // 1. public final void readByte() throws IOException 读取字节数据返回
        // 2. public final void readInt() throws IOException  读取int数据返回
        // 3. public final void readDouble() throws IOException    读取Double数据返回
        // 4. public final void readBoolean() throws IOException    读取Boolean数据返回
        // 5. public final void readUTF(String str) throws IOException 读取字符串(UTF-8)数据返回
        // 6. public void read(byte[])  支持读取字节数据出去

        try (
                // 1. public DataInputStream(InputStream inputStream)    创建新数据输出流包装基础的字节输入流
                DataInputStream dataInputStream= new DataInputStream(new FileInputStream(SAVE_PATH))
        ) {
            // 1. public final void readByte() throws IOException 读取字节数据返回
            System.out.println(dataInputStream.readByte());
            // 2. public final void readInt() throws IOException  读取int数据返回
            System.out.println(dataInputStream.readInt());
            // 3. public final void readDouble() throws IOException    读取Double数据返回
            System.out.println(dataInputStream.readDouble());
            // 4. public final void readBoolean() throws IOException    读取Boolean数据返回
            System.out.println(dataInputStream.readBoolean());
            // 5. public final void readUTF(String str) throws IOException 读取字符串(UTF-8)数据返回
            System.out.println(dataInputStream.readUTF());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 16
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Monly21

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值