IO流(操作基本数据类型的流)

IO流(操作基本数据类型的流)

    DataInputStream :数据输入流
    DataOutputStream:数据输出流

代码:
public static void main(String[] args) throws IOException {
write();
read();

}

private static void read() throws IOException {
//创建数据输入流对象
DataInputStream dis = new DataInputStream(new FileInputStream(“das.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() ;
boolean bb = dis.readBoolean() ;
char ch = dis.readChar() ;

//释放资源
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(bb);
System.out.println(ch);

}

private static void write() throws FileNotFoundException, IOException {
//创建数据输出流对象
DataOutputStream das = new DataOutputStream(new FileOutputStream(“das.txt”));

// 写数据
das.writeByte(10);
das.writeShort(100);
das.writeInt(1000);
das.writeLong(10000L);
das.writeFloat(12.34F);
das.writeDouble(12.56);
das.writeBoolean(true);
das.writeChar('A');

// 释放资源
das.close();

}

}
结果:

10
100
1000
10000
12.34
12.56
true
A

你可能感兴趣的:(IO流(操作基本数据类型的流))