DataInputStream和DataOutputStream的基本使用

本文介绍了Java中字节流和字符流的基本概念,并重点讲解了DataInputStream和DataOutputStream这两个类的功能和使用方法。通过实例演示了如何利用这两个类进行基本数据类型的读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字节流:InputStream类 OutputStream类

字符流:Reader类 Writer类

DataInputStream和DataOutputStream是一对可以直接读取基本类型数据的流,简化了对基本数据类型的读写操作。

DataInputStream主要方法

DataInputStream主要方法:

read(byte[] b) 

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

readBoolean() 

readByte() 

readChar() 

readDouble() 

readFloat() 

readInt() 

readLong() 

readShort() 

readUnsignedByte() 

 int readUnsignedShort() 

 String readUTF() 

static String readUTF(DataInput in) 

 int skipBytes(int n) 

 

DataOutputStream的主要方法

 DataOutputStream的主要方法:

flush() 


size() 


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


write(int b) 


writeBoolean(boolean v) 


writeByte(int v) 


writeBytes(String s) 


writeChar(int v) 


writeChars(String s) 


writeDouble(double v) 


writeFloat(float v) 


writeInt(int v) 


writeLong(long v) 


writeShort(int v) 


writeUTF(String str)

使用方法

        FileOutputStream fos=new FileOutputStream("./logs/data.txt");
		DataOutputStream dos=new DataOutputStream(fos);
		dos.writeInt(88);
		dos.writeUTF("是兄弟就来砍我!");
		dos.close();
 
		FileInputStream fis=new FileInputStream("./logs/data.txt");
		DataInputStream dis=new DataInputStream(fis);
		System.out.println("int:"+dis.readInt());
		System.out.println("UTF:"+dis.readUTF());
        fis.close();

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值