JavaIO流学习总结-DataOutputStream和DataInputStream基本操作练习
package io;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 修改日期:2020/03/31
* 修改人:牟松
* DataOutputStream和DataInputStream基本操作练习
* 修改日期:2020/03/31
* 修改人:牟松
* DataOutputStream和DataInputStream基本操作练习
* 参考连接:https://blog.csdn.net/qq_21808961/article/details/80294246
**/public class Test {
public static void main(String[] args) throws IOException
{
FileOutputStream out = new FileOutputStream("testDataOutputStream.txt");
DataOutputStream dos = new DataOutputStream(out);
byte b = 20;
short s = 30;
int a = 900;
float f = 12.3f;
long l = 800l;
double d = 14.23;
boolean flag = true;
char ch = '中';
{
FileOutputStream out = new FileOutputStream("testDataOutputStream.txt");
DataOutputStream dos = new DataOutputStream(out);
byte b = 20;
short s = 30;
int a = 900;
float f = 12.3f;
long l = 800l;
double d = 14.23;
boolean flag = true;
char ch = '中';
dos.writeByte(b);// 写入byte类型数据
dos.writeShort(s);// 写入short类型数据
dos.writeInt(a);// 写入int类型
dos.writeFloat(f);// 写入float类型
dos.writeLong(l);// 写入long类型
dos.writeDouble(d);// 写入double类型
dos.writeBoolean(flag);
dos.writeChar(ch);// 写入char类型
dos.close();
out.close();
dos.writeShort(s);// 写入short类型数据
dos.writeInt(a);// 写入int类型
dos.writeFloat(f);// 写入float类型
dos.writeLong(l);// 写入long类型
dos.writeDouble(d);// 写入double类型
dos.writeBoolean(flag);
dos.writeChar(ch);// 写入char类型
dos.close();
out.close();
System.out.println(b);
System.out.println(s);
System.out.println(a);
System.out.println(f);
System.out.println(l);
System.out.println(d);
System.out.println(flag);
System.out.println(ch);
System.out.println("------------------------------");
System.out.println(s);
System.out.println(a);
System.out.println(f);
System.out.println(l);
System.out.println(d);
System.out.println(flag);
System.out.println(ch);
System.out.println("------------------------------");
FileInputStream in = new FileInputStream("testDataOutputStream.txt");
DataInputStream dif = new DataInputStream(in);
byte btemp;
short stemp;
int atemp;
float ftemp;
long ltemp;
double dtemp;
boolean flagtemp;
char chtemp;
DataInputStream dif = new DataInputStream(in);
byte btemp;
short stemp;
int atemp;
float ftemp;
long ltemp;
double dtemp;
boolean flagtemp;
char chtemp;
btemp = dif.readByte();
stemp = dif.readShort();
atemp = dif.readInt();
ftemp = dif.readFloat();
ltemp = dif.readLong();
dtemp = dif.readDouble();
flagtemp = dif.readBoolean();
chtemp = dif.readChar();
stemp = dif.readShort();
atemp = dif.readInt();
ftemp = dif.readFloat();
ltemp = dif.readLong();
dtemp = dif.readDouble();
flagtemp = dif.readBoolean();
chtemp = dif.readChar();
System.out.println(btemp);
System.out.println(stemp);
System.out.println(atemp);
System.out.println(ftemp);
System.out.println(ltemp);
System.out.println(dtemp);
System.out.println(flagtemp);
System.out.println(chtemp);
dif.close();
in.close();
}
}
System.out.println(stemp);
System.out.println(atemp);
System.out.println(ftemp);
System.out.println(ltemp);
System.out.println(dtemp);
System.out.println(flagtemp);
System.out.println(chtemp);
dif.close();
in.close();
}
}