package com.test.test;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataInputstream {
public static void main(String[] args) throws Exception {
FileOutputStream out = new FileOutputStream("D:/a.dat");
//DataOutputStream可以将各种各样的数据转换为二进制
DataOutputStream dout = new DataOutputStream(out);
String name = "zhangsan";
int num = 100;
float f = 100.8f;
double d = 10088.00d;
//为了让解析工具知道这个字符串有多长,还会在字符串前面加前缀。
//表示这个字符串有多长,8个字节的字符串,还有两个字节的前缀,会写入10个字节
dout.writeUTF(name);
//4个字节
dout.writeInt(num);
//4个字节
dout.writeFloat(f);
//8个字节
dout.writeDouble(d);
FileInputStream in = new FileInputStream("D:/a.dat");
DataInputStream din = new DataInputStream(in);
String readUTF = din.readUTF();
System.out.println(readUTF);
int readInt = din.readInt();
System.out.println(readInt);
float readFloat = din.readFloat();
System.out.println(readFloat);
double readDouble = din.readDouble();
System.out.println(readDouble);
}
}