这两个类完成二进制licenses的生成和解析。
/**
* Java读取解析二进制数据
*
* @author leizhimin 2008-10-14 12:42:51
*/
public class TestLicenses {
public static void main(String[] args) throws IOException {
testDataOutputStream();
testDataInputStream();
}
/**
* 生成二进制数据文件
*
* @throws IOException
*/
public static void testDataOutputStream() throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("C:\\dos.dat"));
dos.writeBoolean(true);
dos.writeByte('a');
dos.writeDouble(234.44);
dos.writeFloat(11.23f);
dos.writeLong(555L);
dos.writeInt(333);
dos.writeShort(23);
dos.writeUTF("UTFString");
dos.close();
}
/**
* 解析二进制数据文件
*
* @throws IOException
*/
public static void testDataInputStream() throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream("C:\\dos.dat"));
System.out.println(dis.readBoolean());
System.out.println((char) dis.readByte());
System.out.println(dis.readDouble());
System.out.println(dis.readFloat());
System.out.println(dis.readLong());
System.out.println(dis.readInt());
System.out.println(dis.readShort());
System.out.println(dis.readUTF());
dis.close();
}
}
a
234.44
11.23
555
333
23
UTFString
Process finished with exit code 0