DataInputStream 和 DataOutputStream 2020-08-23

 

DataInputStream

DataInputStream能以一种与机器无关(当前操作系统等)的方式,直接地从字节流读取Java基本数据类型String类型的数据类型的数据,常用语网络传输(网络传输数据要求与平台无关)。

DataOutputStream写的文件,只能使用DataInputStream去读。并且去读的时候你需要提前知道写入的顺序,读写顺序一致才能正常取出数据

 
Constructor and Description
DataInputStream(InputStream in)

创建使用指定的底层InputStream的DataInputStream。

Modifier and TypeMethod and Description
intread(byte[] b)

从包含的输入流中读取一些字节数,并将它们存储到缓冲区数组 b

intread(byte[] b, int off, int len)

从包含的输入流读取最多 len个字节的数据为字节数组。

booleanreadBoolean()

见的总承包 readBoolean的方法 DataInput

bytereadByte()

见的总承包 readByte的方法 DataInput

charreadChar()

readChar方法的总合同 DataInput

doublereadDouble()

readDouble方法 DataInput的总体合同。

floatreadFloat()

readFloatDataInput的一般合同。

voidreadFully(byte[] b)

见的总承包 readFully的方法 DataInput

voidreadFully(byte[] b, int off, int len)

见的总承包 readFully的方法 DataInput

intreadInt()

readInt方法 DataInput的一般合同。

longreadLong()

见的总承包 readLong的方法 DataInput

shortreadShort()

readShort方法 DataInput的一般合同。

intreadUnsignedByte()

见的总承包 readUnsignedByte的方法 DataInput

intreadUnsignedShort()

readUnsignedShortDataInput的一般合同。

StringreadUTF()

readUTFDataInput的一般合同。

static StringreadUTF(DataInput in)

从流in读取以modified UTF-8格式编码的Unicode字符串的表示; 这个字符串然后作为String返回。

intskipBytes(int n)

skipBytesDataInput的一般合同。

public class DataInputStreamTest01 {
    public static void main(String[] args) {
        DataInputStream dis = null;
        try {
            dis = new DataInputStream(new FileInputStream("..\\..\\java\\data"));
            /*读出来是乱码行不通
            byte[] bytes = new byte[1024];
            int readCount = 0;
            while ((readCount = dis.read(bytes)) != -1){
                System.out.println(new String(bytes,0,readCount));
            }
            */
            System.out.println(dis.readByte());
            System.out.println(dis.readShort());
            System.out.println(dis.readInt());
            System.out.println(dis.readLong());
            System.out.println(dis.readFloat());
            System.out.println(dis.readDouble());
            System.out.println(dis.readBoolean());
            System.out.println(dis.readChar());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (dis != null){
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 


​​​​​​​DataOutputStream

​​​​​​​DataInputStream则可以直接将Java基本数据类型和String类型写入到其他字节输入流。

这个文件并不是普通文本文档(记事本打不开)​​​​​​​

构造方法:

Modifier and TypeField and Description
protected intwritten

到目前为止写入数据输出流的字节数。

常用方法:​​​​​​​

Modifier and TypeMethod and Description
voidflush()

刷新此数据输出流。

intsize()

返回计数器的当前值 written ,到目前为止写入此数据输出流的字节数。

voidwrite(byte[] b, int off, int len)

写入 len从指定的字节数组起始于偏移 off基础输出流。

voidwrite(int b)

将指定的字节(参数 b的低8位)写入底层输出流。

voidwriteBoolean(boolean v)

boolean写入底层输出流作为1字节值。

voidwriteByte(int v)

byte作为1字节值写入底层输出流。

voidwriteBytes(String s)

将字符串作为字节序列写入基础输出流。

voidwriteChar(int v)

char写入底层输出流作为2字节值,高字节优先。

voidwriteChars(String s)

将字符串写入底层输出流作为一系列字符。

voidwriteDouble(double v)

双参数传递给转换 long使用 doubleToLongBits方法在类 Double ,然后写入该 long值基础输出流作为8字节的数量,高字节。

voidwriteFloat(float v)

浮子参数的转换 int使用 floatToIntBits方法在类 Float ,然后写入该 int值基础输出流作为一个4字节的数量,高字节。

voidwriteInt(int v)

将底层输出流写入 int作为四字节,高位字节。

voidwriteLong(long v)

long写入底层输出流,为8字节,高字节为首。

voidwriteShort(int v)

short写入底层输出流作为两个字节,高字节优先。

voidwriteUTF(String str)

使用 modified UTF-8编码以机器无关的方式将字符串写入基础输出流。

 

public class DataOutputStreamTest01 {
    public static void main(String[] args) {
        //创建数据专属的字节输出流
        DataOutputStream dos = null;
        try {
            dos = new DataOutputStream(new FileOutputStream("..\\..\\java\\data"));

            //写
            byte b = 97;
            short s = 100;
            int i = 300;
            long l = 500;
            float f = 9.0F;
            double d = 3.14;
            boolean sex = false;
            char c = 'a';
            dos.writeByte(b);
            dos.writeShort(s);
            dos.writeInt(i);
            dos.writeLong(l);
            dos.writeFloat(f);
            dos.writeDouble(d);
            dos.writeBoolean(sex);
            dos.writeChar(c);
            //刷新
            dos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (dos != null){
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值