1. 程式人生 > >java基礎類庫學習(六.3)位元組流 字元流(輸入輸出)

java基礎類庫學習(六.3)位元組流 字元流(輸入輸出)

前言:

位元組流和字元流的操作方式幾乎相同,區別只是操作的資料單元不同而已

位元組流操作的資料單元是位元組8位,字元流操作的資料單元是字元16位

輸入流

字元輸入流/位元組輸入流原始碼(輸入流的抽象基類)

InputStream

public abstract class InputStream implements Closeable {
private static final int MAX_SKIP_BUFFER_SIZE = 2048;
public abstract int read() throws IOException;
public int read(byte b[]) throws IOException {
    return read(b, 0, b.length);
}
public void close() throws IOException {}
public synchronized void mark(int readlimit) {}
public int read(byte b[], int off, int len) throws IOException {
    if (b == null) {
        throw new NullPointerException();
    } else if (off < 0 || len < 0 || len > b.length - off) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    }

    int c = read();
    if (c == -1) {
        return -1;
    }
    b[off] = (byte)c;

    int i = 1;
    try {
        for (; i < len ; i++) {
            c = read();
            if (c == -1) {
                break;
            }
            b[off + i] = (byte)c;
        }
    } catch (IOException ee) {
    }
    return i;
}
public long skip(long n) throws IOException {

    long remaining = n;
    int nr;

    if (n <= 0) {
        return 0;
    }

    int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
    byte[] skipBuffer = new byte[size];
    while (remaining > 0) {
        nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
        if (nr < 0) {
            break;
        }
        remaining -= nr;
    }

    return n - remaining;
}
  public boolean markSupported() {
        return false;
    }

}

Reader

public abstract class Reader implements Readable, Closeable {
protected Object lock;
protected Reader() {
    this.lock = this;
}
public int read(java.nio.CharBuffer target) throws IOException {
    int len = target.remaining();
    char[] cbuf = new char[len];
    int n = read(cbuf, 0, len);
    if (n > 0)
        target.put(cbuf, 0, n);
    return n;
}
abstract public int read(char cbuf[], int off, int len) throws IOException;
public boolean markSupported() {
    return false;
}
   abstract public void close() throws IOException;

}
可以發現Reader和InputStream中的方法幾乎一致

輸出流

位元組輸出流/字元輸出流(輸出流的抽象基類)

OutputStream

public abstract class OutputStream implements Closeable, Flushable {
 
public abstract void write(int b) throws IOException;
public void write(byte b[]) throws IOException {
    write(b, 0, b.length);
}
public void flush() throws IOException {
}
 public void close() throws IOException {
    }

}

Writer

public abstract class Writer implements Appendable, Closeable, Flushable {
private char[] writeBuffer;
private static final int WRITE_BUFFER_SIZE = 1024;
protected Object lock;
protected Writer() {
    this.lock = this;
}
protected Writer(Object lock) {
    if (lock == null) {
        throw new NullPointerException();
    }
    this.lock = lock;
}
abstract public void write(char cbuf[], int off, int len) throws IOException;

  abstract public void close() throws IOException;

}