基本数据类型的流操作

对于基本数据类型的输入输出流操作,java专门提供了一组类:DataInputStream和DataOutputstream


构造函数:

——1,DataInputStream(InputStream  in)

——2,DataOutputStream(OutputStream  out)



特有方法:

readInt()---writeInt(int  a)

readDouble()---writeDouble(double  a)

readBoolean()---writeBoolean(boolean  a)

readUTF()---writeUTF(String  str)//以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流



演示代码:

import java.io.*;
class DataStreamDemo 
{
	public static void main(String[] args) throws IOException
	{
		write();
		read();
	}
	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
	public static void write() throws IOException
	{
		DataOutputStream dos=new DataOutputStream(new FileOutputStream("Data.txt"));
		
		dos.writeInt(4);
		dos.writeDouble(15.6);
		dos.writeBoolean(true);
		dos.writeUTF("呵呵");

		dos.close();
	}
	public static void read() throws IOException
	{
		DataInputStream dis=new DataInputStream(new FileInputStream("Data.txt"));
		
		//按顺序读取
		sop(dis.readInt());
		sop(dis.readDouble());
		sop(dis.readBoolean());
		sop(dis.readUTF());

		dis.close();
	}
}
/*
结果:
4
15.6
true
呵呵
*/

文件:



由于输入输出是字节流,到记事本中会查表转换成字符,所以呈现乱码属于正常现象



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值