IO流 | DataInputStream和DataOutputStream

过滤器字节流:

  • FilterInputStream:过滤器字节输入流
  • FilterOutputStream:过滤器字节输出流

它们的主要用于封装其他的输入输出流,为它们提供一些额外的功能。

  • 具有以下实现类:
    在这里插入图片描述
    ☛以下以FilterInputStream做主要分析:

FilterInputStream

1. 源码分析

(1)继承关系 该类继承自InputStream父类(表示该类可实现父类中的相关方法)

public class FilterInputStream extends InputStream

(2)成员变量

protected volatile InputStream in;

(3)构造方法

protected FilterInputStream(InputStream in) {
        this.in = in;
    }

由构造方法可以看出,FilterInputStream属于高级流,在实例化时需要传入InputStream对象
(4)基本方法

//每次读取一个字节
public int read() throws IOException;

//批量读取数据到b数组中
public int read(byte b[]) throws IOException;

//从off位置开始,批量读取len长度的数据
public int read(byte b[], int off, int len) throws IOException;

//跳过n个字节
public long skip(long n) throws IOException;

//获取数据流中数据字节个数
public int available() throws IOException ;

//关闭资源
public void close() throws IOException;

//设置标记 在流中设置一个标记后最多只能读取readlimit个字节,否则标记可能失效
public synchronized void mark(int readlimit) ;

//重置
public synchronized void reset() throws IOException;

//判断当前系统是否支持标记
public boolean markSupported();

一、DataInputStream

类似FileInputStream的各种流,只能用来输入/输出字节/字符类型的数据,对于其他类型的数据,无法直接操作,在输出时需要先将其转换为字节或字符类型再输出,输入时按照字节或字符类型读入之后再转换为原来的类型,工作量较大,故引入数据流来输入/输出各种类型的数据。

构造方法:继承FilterInputStream类
public
class DataInputStream extends FilterInputStream implements DataInput {

    public DataInputStream(InputStream in) {
        super(in);
    }
常用方法:
  1. 读取boolean数据类型
public final boolean readBoolean() throws IOException {
        int ch = in.read();
        if (ch < 0)
            throw new EOFException();
        return (ch != 0);
    }
  1. 读取int数据类型:一次性读取出4个字节,再进行处理成一个int
public final int readInt() throws IOException {
        int ch1 = in.read();
        int ch2 = in.read();
        int ch3 = in.read();
        int ch4 = in.read();
        if ((ch1 | ch2 | ch3 | ch4) < 0)
            throw new EOFException();
        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
    }
  1. 读取String数据类型
public final static String readUTF(DataInput in) throws IOException {
        //从in中读取“无符号short类型”的值
        int utflen = in.readUnsignedShort();
        byte[] bytearr = null;
        char[] chararr = null;
        }
  1. 读取其他类型方法
//读short类型数据
public final short readShort() throws IOException;

//读char类型数据
public final char readChar() throws IOException

//读long类型数据
public final long readLong() throws IOException

//读float类型数据
public final float readFloat() throws IOException

//读double类型数据
public final double readDouble() throws IOException

//读一行数据
public final String readLine() throws IOException

二、DataOutputStream

构造方法:
public
class DataOutputStream extends FilterOutputStream implements DataOutput {
   
    public DataOutputStream(OutputStream out) {
        super(out);
    }
常用方法:
//单个字节写入操作
public synchronized void write(int b) throws IOException;

//byte数组中off位置开始,批量读取len长度个数据,写入存储介质中
public synchronized void write(byte b[], int off, int len) throws IOException;

//写布尔数据类型
public final void writeBoolean(boolean v) throws IOException;

//写byte字节类型数据
public final void writeByte(int v) throws IOException;

//写short类型数据
public final void writeShort(int v) throws IOException;

//写char类型数据
public final void writeChar(int v) throws IOException;

//写int类型数据
public final void writeInt(int v) throws IOException;

//写long类型数据
public final void writeLong(long v) throws IOException;

//写float类型数据
public final void writeFloat(float v) throws IOException;

//写double类型数据
public final void writeDouble(double v) throws IOException;

//写utf类型数据
public final void writeUTF(String str) throws IOException;

注意: 依次读取数据时需要按照写入的类型顺序读取

三、自定义测试Demo使用DataXXXStream

public class DataStreamDemo {
    public static void dataStreamOperation(String path){
        try {
            //写操作
            DataOutputStream out = new DataOutputStream(new FileOutputStream(path));
            //写入int类型的数据
            out.writeInt(123);
            //写入byte类型的数据
            out.writeByte('c');
            //写入double类型数据
            out.writeDouble(12.98);

            //写入字符串数据
            out.writeUTF("helloworld");
            out.writeUTF("oneday");
            
            //写入boolean类型的数据
            out.writeBoolean(false);

            //关闭资源
            out.flush(); //刷新此缓冲的输出流,保证数据全部都能写出
            out.close();

            //读操作
            DataInputStream in = new DataInputStream(new FileInputStream(path));
            
            //读int类型
            int a = in.readInt();
            //读byte类型
            byte b=in.readByte();
            //读double类型
            double v = in.readDouble();
            System.out.println("int:"+a+" byte:"+b+" double:"+v);
            //读字符串类型
            String c = in.readUTF();
            String e = in.readUTF();
            System.out.println("string:1."+c+" 2."+e);
            //读取boolean数据类型
            boolean d= in.readBoolean();
            System.out.println("boolean:"+d);
            //关闭资源
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //DataInputStream可以直接从数据流中读取int,String等类型的数据
    public static void main(String[] args) {
        String path="C:\\Users\\1\\Desktop\\test2.txt";
        dataStreamOperation(path);
    }
}

运行结果:
在这里插入图片描述

四、DataXXXStream适用场景:

需要读取或写入类型较多,例如:Excle表输入不同类型数据(int、String、boolean等)可以使用Data流

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值