JAVA IO篇之DataInputStream ,DataOutputStream

为什么要说下这两个类,是这两个类为我们提供了一些方便的方法操作数据

1、DataInputStream (用来读取流中的字节数据并转换为JAVA基本类型)

182131_AfNN_2663859.png

上面方法可以看到,从流中读取字节转换为java基本类型,列如:

public final int readInt() throws IOException {
    int ch1 = in.read();
    int ch2 = in.read();
    int ch3 = in.read();
    int ch4 = in.read();
    if ((ch1 | ch2 | ch3 | ch4) < 0)
        throw new EOFException();
    return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}

java 中int 是占用4个字节长度,所以读取4个字节转换为int 基本类型

其实,这个类我们平常基本你不会用到

2、DataOutputStream (这个类可以把Java的基本类型通过字节流输入),其实这个类对于我们来说还是有点作用的

比如:你要把一个话(日志信息)记录在一个文件,你可以通过这个类写入进去,这样很方便

try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File("D://array.txt")))){
    dos.write("测试中!!!".getBytes());
}catch (Exception e){
    e.printStackTrace();
}

 

转载于:https://my.oschina.net/chenping12/blog/1480212

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值