IO_特殊流源码解析

 

DataOutputStream数据输出流 将java基本数据类型写入数据输出流中,并可以通过数据输入流DataInputStream将数据读入。

分类字节输入流字节流输出流
特殊流DataInoutStreamDataInputStream

 

应用场景:可以直接输出float类型和long类型等,通过使用该流可以提高数据读写的效率

DataInputStream源码浅析

继承关系:

public class ObjectInputStream
    extends InputStream implements ObjectInput, ObjectStreamConstants{}
//继承自InputStream类,实现了ObjectInput, ObjectStreamConstants接口

基本属性:

  // 字节数组
  private byte bytearr[] = new byte[80];
  // 字符数组
  private char chararr[] = new char[80];

构造函数和基本方法:

  // 有参数构造方法,传入底层的输入流
  public DataInputStream(InputStream in) {
    super(in);
  }
  // 将数据从数据输入流中读取到字节数组b中
  public final int read(byte b[]) throws IOException {
    return in.read(b, 0, b.length);
  }
 
  // 将数据从数据输入流中读取到字节数组b中,
  //b的起始位置是off,长度是len
  public final int read(byte b[], int off, int len) throws IOException {
    return in.read(b, off, len);
  }
 
  // 从数据输入流中读取数据,填充字节数组b.
  public final void readFully(byte b[]) throws IOException {
    readFully(b, 0, b.length);
  }
 
  // 将数据从数据输入流读取到字节数组b中,起始位置是off,长度len.
  // 此方法会循环从输入流读取len个字节.
  public final void readFully(byte b[], int off, int len) throws IOException {
    if (len < 0)
      throw new IndexOutOfBoundsException();
    int n = 0;
    while (n < len) {
      int count = in.read(b, off + n, len - n);
      if (count < 0)
        throw new EOFException();
      n += count;
    }
  }
 
  // 跳过n个字节
  public final int skipBytes(int n) throws IOException {
    int total = 0;
    int cur = 0;
 
    while ((total < n) && ((cur = (int) in.skip(n - total)) > 0)) {
      total += cur;
    }
 
    return total;
  }
 
  // 从数据输入流中读取布尔类型.
  public final boolean readBoolean() throws IOException {
    int ch = in.read();
    if (ch < 0)
      throw new EOFException();
    return (ch != 0);
  }
 
  // 从数据输入流中读取一个字节
  public final byte readByte() throws IOException {
    int ch = in.read();
    if (ch < 0)
      throw new EOFException();
    return (byte) (ch);
  }
 
  // 从数据输入流中读取一个无符号的字节
  public final int readUnsignedByte() throws IOException {
    int ch = in.read();
    if (ch < 0)
      throw new EOFException();
    return ch;
  }
 
  // 从数据输入流中读取一short类型数据
  public final short readShort() throws IOException {
    int ch1 = in.read();
    int ch2 = in.read();
    if ((ch1 | ch2) < 0)
      throw new EOFException();
    return (short) ((ch1 << 8) + (ch2 << 0));
  }
 
  // 从数据输入里中读取一个无符号short类型数据
  public final int readUnsignedShort() throws IOException {
    int ch1 = in.read();
    int ch2 = in.read();
    if ((ch1 | ch2) < 0)
      throw new EOFException();
    return (ch1 << 8) + (ch2 << 0);
  }
 
  // 从数据输入流中读取一个字符数据
  public final char readChar() throws IOException {
    int ch1 = in.read();
    int ch2 = in.read();
    if ((ch1 | ch2) < 0)
      throw new EOFException();
    return (char) ((ch1 << 8) + (ch2 << 0));
  }
 
  // 从数据输入流中读取一个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));
  }
 
  private byte readBuffer[] = new byte[8];
 
  // 从数据输入流中读取一个long类型的数据
  public final long readLong() throws IOException {
    readFully(readBuffer, 0, 8);
    return (((long) readBuffer[0] << 56) + ((long) (readBuffer[1] & 255) << 48) + ((long) (readBuffer[2] & 255) << 40)
        + ((long) (readBuffer[3] & 255) << 32) + ((long) (readBuffer[4] & 255) << 24) + ((readBuffer[5] & 255) << 16)
        + ((readBuffer[6] & 255) << 8) + ((readBuffer[7] & 255) << 0));
  }
 
  // 从数据输入流中读取一个float类型的数据
  public final float readFloat() throws IOException {
    return Float.intBitsToFloat(readInt());
  }
 
  // 从数据输入流中读取一个double类型的数据
  public final double readDouble() throws IOException {
    return Double.longBitsToDouble(readLong());
  }
 
  private char lineBuffer[];

DataOutputStream源码浅析:

继承关系

public
class DataOutputStream extends FilterOutputStream implements DataOutput {}
//继承自FilterOutputStream类,实现了DataOutput接口

基本属性

  // 写到数据输出流中字节数
  protected int written;
  // 数据输出流中的字节数组
  private byte[] bytearr = null;

构造函数和基本方法

// 构造方法.参数传入的是基础输出流
  public DataOutputStream(OutputStream out) {
    super(out);
  }
 
  // 数据输出流的字节数增加,value是增加的字节数
  private void incCount(int value) {
    int temp = written + value;
    if (temp < 0) {
      temp = Integer.MAX_VALUE;
    }
    written = temp;
  }
 
  // 将int类型数据b写到数据输出流中
  public synchronized void write(int b) throws IOException {
    out.write(b);
    incCount(1);
  }

  // 将字节数组b,off索引位置开始,len个长度写到数据输出流中
  public synchronized void write(byte b[], int off, int len) throws IOException {
    out.write(b, off, len);
    incCount(len);
  }
 
  // 刷新数据输出流
  public void flush() throws IOException {
    out.flush();
  }
 
  // 将boolean类型的数据写到数据输出流中
  public final void writeBoolean(boolean v) throws IOException {
    out.write(v ? 1 : 0);
    incCount(1);
  }
 
  // 将字节数据写到数据输出流中
  public final void writeByte(int v) throws IOException {
    out.write(v);
    incCount(1);
  }
 
  // 将short类型数据写到数据输出流中
  // short占两个字节,将高8位和低8位分别存储
  public final void writeShort(int v) throws IOException {
    out.write((v >>> 8) & 0xFF);
    out.write((v >>> 0) & 0xFF);
    incCount(2);
  }
 
  // 将字符类型的数据写到数据输出流中
  // char占两个字节,将高8位和低8位分别存储
  public final void writeChar(int v) throws IOException {
    out.write((v >>> 8) & 0xFF);
    out.write((v >>> 0) & 0xFF);
    incCount(2);
  }
 
  // 将int类型的数据写到数据输出流中
  // int占4个字节.所以将4个字节分别存储
  public final void writeInt(int v) throws IOException {
    out.write((v >>> 24) & 0xFF);
    out.write((v >>> 16) & 0xFF);
    out.write((v >>> 8) & 0xFF);
    out.write((v >>> 0) & 0xFF);
    incCount(4);
  }
 
  private byte writeBuffer[] = new byte[8];
 
  // 将long类型数据写到数据输出流
  // long是8个字节,将8个字节分别存储.
  public final void writeLong(long v) throws IOException {
    writeBuffer[0] = (byte) (v >>> 56);
    writeBuffer[1] = (byte) (v >>> 48);
    writeBuffer[2] = (byte) (v >>> 40);
    writeBuffer[3] = (byte) (v >>> 32);
    writeBuffer[4] = (byte) (v >>> 24);
    writeBuffer[5] = (byte) (v >>> 16);
    writeBuffer[6] = (byte) (v >>> 8);
    writeBuffer[7] = (byte) (v >>> 0);
    out.write(writeBuffer, 0, 8);
    incCount(8);
  }
 
  // 将float类型的数据写到数据输出流中
  public final void writeFloat(float v) throws IOException {
    writeInt(Float.floatToIntBits(v));
  }
 
  // 将double类型的数据写到数据输出流中
  public final void writeDouble(double v) throws IOException {
    writeLong(Double.doubleToLongBits(v));
  }
 
  // 将字符串转换成字节写到数据输出流中
  public final void writeBytes(String s) throws IOException {
    int len = s.length();
    for (int i = 0; i < len; i++) {
      out.write((byte) s.charAt(i));
    }
    incCount(len);
  }
 
  // 将字节数据转换成字符写到数据输出流中
  public final void writeChars(String s) throws IOException {
    int len = s.length();
    for (int i = 0; i < len; i++) {
      int v = s.charAt(i);
      out.write((v >>> 8) & 0xFF);
      out.write((v >>> 0) & 0xFF);
    }
    incCount(len * 2);
  }

写一个运用的例子:

import java.io.*;

public class DataDemo {
    /**
     * DataOutputStream & DataInputStream 用于操作基本数据类型
     */
    public static void main(String[] args) throws IOException {

        writeDemo();
        readDemo();
    }
    public static void readDemo() throws IOException {
        DataInputStream dos = new DataInputStream(new FileInputStream("data.txt"));
        String s = dos.readUTF();
        System.out.println(s);
    }

    public static void writeDemo() throws IOException {
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
        dos.writeUTF("你好啊");
    }
}

/**
*运行结果
*
*你好啊
*
*Process finished with exit code 0
*/

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值