java–基础–16.6–IO流–DataInputStream,DataOutputStream
1、介绍
可以读写基本数据类型的数据
1.1、数据输入流:DataInputStream
- DataInputStream(InputStream in)
1.2、数据输出流:DataOutputStream
- DataOutputStream(OutputStream out)
2、案例
// 创建数据输出流对象
DataOutputStream dos = new DataOutputStream(new FileOutputStream(
"test.txt"));
// 写数据了
dos.writeByte(1);
dos.writeShort(2);
dos.writeInt(3);
dos.writeLong(4);
dos.writeFloat(5.0f);
dos.writeDouble(6.0);
dos.writeChar('a');
dos.writeBoolean(true);
// 释放资源
dos.close();
DataInputStream dis = new DataInputStream( new FileInputStream("test.txt"));
// 读数据
byte b = dis.readByte();
short s = dis.readShort();
int i = dis.readInt();
long l = dis.readLong();
float f = dis.readFloat();
double d = dis.readDouble();
char c = dis.readChar();
boolean bb = dis.readBoolean();
// 释放资源
dis.close();
System.out.println(b);
System.out.println(s);
System.out.println(i);
System.out.println(l);
System.out.println(f);
System.out.println(d);
System.out.println(c);
System.out.println(bb);
输出:
1
2
3
4
5.0
6.0
a
true