快速理解 Java的流(Stream)

14 篇文章 1 订阅

什么是流

流,先来个中文组词: 水流

水流: 很多的水分子组成,就从一个方向统一向另一个方向运动的过程

在这里插入图片描述

很显然,上面的图片,水流是从上向下流动,对吧

既然理解了什么是水流,那么我们现在来讲讲Java中的流

计算机中的流…流的是啥? 流的是数据,也就是一串一串的0100100100111011001011001010…

输入流和输出流

欸!那么我们可以根据流向,可以分为输入流输出流

都知道,水流是从高向低处流动,那么我们输入输出,是针对于什么的输入输出呢??

我们都知道,Java是运行在Java虚拟机上的,而Java虚拟机又是运行在内存上的,所以,对于Java中的流来讲…是针对于当前运行的Java程序的内存

从外部进入到内存的数据,叫做输入流;从内存中流向其他地方的数据,叫做输出流

在这里插入图片描述

字节流和字符流

这么分…是根据数据的流向分,那么在Java中,除了这种分法,

还可以通过处理的数据单位的不同进行划分,这就可以分为字节流字符流

什么是字节 什么是字符…这里就不多赘述了

字符…在计算机中可以用字节表示,也就是说,每个字符都是由多个byte(字节)组成,那么我就可以直接使用字节就够了,我干嘛还需要字符呢?

如果你是在传输图片,传输视频等等这些数据的话…用字节流,那是毋庸置疑的

但是,如果是传输大量的文本数据呢?

以utf-8编码格式为例,utf-8编码最大可是4个字节哦!也就是说一个本来只需要按照一个字符处理的部分,还需要先读进来4个字节之后再组成一个字符…

还有一个问题…大部分的时候这些流都是从网络上撸过来的,万一…他出现了网络抖动…掉了一两个字节呢?对于图片来讲无伤大雅,但是对于一个字符来讲,整个就变了啊!!

所以,在Java中,一般文本的传输采用字符流的形式

节点流和处理流

节点流:节点流从一个特定的数据源读写数据

处理流:通过对数据的处理为程序提供更为强大的读写功能(“连接”在已存在的流之上)

节点流…你可以理解为点对点的流处理,直接往文件中写数据

处理流…就是在已经存在的一个流上,添加一些其他功能

还不太理解?

我需要给一个瀑布下面的那个湖水染色,怎么办?两种办法:

1.我直接在瀑布顶上或者直接在湖水里染色(节点流)

2.在瀑布的流水中染色(处理流)

在这里插入图片描述
当然…不管是缓冲流还是节点流…你都必须用完就关,不要占用着空间不释放好吧,在这里替内存谢谢你

Java的流相关类型

在java.io包中: 这么几个抽象类

字节流字符流
输入流InputStream.javaReader.java
输出流OutputStream.javaWriter.java
字节流的:

对于InputStream这个抽象类来讲:

在这里插入图片描述

对于OutputStream这个抽象类来讲:

在这里插入图片描述

字符流的:

对于Reader来讲:

在这里插入图片描述

对于Writer来讲:

在这里插入图片描述

InputStream.java源码:

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 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 int available() throws IOException {
        return 0;
    }

    public void close() throws IOException {}
    
    public synchronized void mark(int readlimit) {}
    
    public synchronized void reset() throws IOException {
        throw new IOException("mark/reset not supported");
    }
    
    public boolean markSupported() {
        return false;
    }
}

OutputStream.java源码:

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 write(byte b[], int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if ((off < 0) || (off > b.length) || (len < 0) ||
                   ((off + len) > b.length) || ((off + len) < 0)) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return;
        }
        for (int i = 0 ; i < len ; i++) {
            write(b[off + i]);
        }
    }

    public void flush() throws IOException {
    }

    public void close() throws IOException {
    }

}

Reader.java源码

public abstract class Reader implements Readable, Closeable {

    protected Object lock;

    protected Reader() {
        this.lock = this;
    }

    protected Reader(Object lock) {
        if (lock == null) {
            throw new NullPointerException();
        }
        this.lock = lock;
    }

    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;
    }

    public int read() throws IOException {
        char cb[] = new char[1];
        if (read(cb, 0, 1) == -1)
            return -1;
        else
            return cb[0];
    }

    public int read(char cbuf[]) throws IOException {
        return read(cbuf, 0, cbuf.length);
    }

    abstract public int read(char cbuf[], int off, int len) throws IOException;

    private static final int maxSkipBufferSize = 8192;

    private char skipBuffer[] = null;

    public long skip(long n) throws IOException {
        if (n < 0L)
            throw new IllegalArgumentException("skip value is negative");
        int nn = (int) Math.min(n, maxSkipBufferSize);
        synchronized (lock) {
            if ((skipBuffer == null) || (skipBuffer.length < nn))
                skipBuffer = new char[nn];
            long r = n;
            while (r > 0) {
                int nc = read(skipBuffer, 0, (int)Math.min(r, nn));
                if (nc == -1)
                    break;
                r -= nc;
            }
            return n - r;
        }
    }

    public boolean ready() throws IOException {
        return false;
    }

    public boolean markSupported() {
        return false;
    }

    public void mark(int readAheadLimit) throws IOException {
        throw new IOException("mark() not supported");
    }

    public void reset() throws IOException {
        throw new IOException("reset() not supported");
    }
    
    abstract public void close() throws IOException;

}

Writer.java源码

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;
    }

    public void write(int c) throws IOException {
        synchronized (lock) {
            if (writeBuffer == null){
                writeBuffer = new char[WRITE_BUFFER_SIZE];
            }
            writeBuffer[0] = (char) c;
            write(writeBuffer, 0, 1);
        }
    }

    public void write(char cbuf[]) throws IOException {
        write(cbuf, 0, cbuf.length);
    }

    abstract public void write(char cbuf[], int off, int len) throws IOException;

    public void write(String str) throws IOException {
        write(str, 0, str.length());
    }

    public void write(String str, int off, int len) throws IOException {
        synchronized (lock) {
            char cbuf[];
            if (len <= WRITE_BUFFER_SIZE) {
                if (writeBuffer == null) {
                    writeBuffer = new char[WRITE_BUFFER_SIZE];
                }
                cbuf = writeBuffer;
            } else {    // Don't permanently allocate very large buffers.
                cbuf = new char[len];
            }
            str.getChars(off, (off + len), cbuf, 0);
            write(cbuf, 0, len);
        }
    }

    public Writer append(CharSequence csq) throws IOException {
        if (csq == null)
            write("null");
        else
            write(csq.toString());
        return this;
    }

    public Writer append(CharSequence csq, int start, int end) throws IOException {
        CharSequence cs = (csq == null ? "null" : csq);
        write(cs.subSequence(start, end).toString());
        return this;
    }

    public Writer append(char c) throws IOException {
        write(c);
        return this;
    }

    abstract public void flush() throws IOException;

    abstract public void close() throws IOException;

}

以上的源码…如果有能力理解的就可以根据源码来理解,如果不能理解的兄弟集美们,查查资料,知道如何使用即可











感谢您的浏览,如果觉得还不错,点个赞呗~😘

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值