为什么要说下这两个类,是这两个类为我们提供了一些方便的方法操作数据
1、DataInputStream (用来读取流中的字节数据并转换为JAVA基本类型)
上面方法可以看到,从流中读取字节转换为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(); }