Java IO系列0 InputStream与OutputStream(字节流)

来源:互联网 发布:加盟农村淘宝要多少钱 编辑:程序博客网 时间:2024/06/15 14:44


该系列的分析源码基于Java 1.8.0_45

一、InputStream 
public abstract class InputStream extends Object implements Closeable
Since:
JDK1.0

 private static final int MAX_SKIP_BUFFER_SIZE = 2048;
 输入流是可以跳过指定字节,但是我们不可能跳过很长的字节,2048就是最大的值


  public abstract int read() throws IOException;
  读取下一个位置的字节,返回读取的字节,如果返回-1,说明已经到达流的结尾


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

       循环把数据读取到b[]字节数组里,位置从1开始,最多读取b.length个字节
       该方法返回读入缓冲区的总字节数


     public int read(byte b[], int off, int len) throws IOException
       循环把数据读取到b[]字节数组里,位置从off开始,最多读取b.length个字节
       该方法返回读入缓冲区的总字节数


      public long skip(long n) throws IOException
        跳过指定最大字节
        返回真实跳过字节数


       public int available() throws IOException

     public synchronized void mark(int readlimit) 

     public boolean markSupported() 
     

     public synchronized void reset() throws IOException

     public void close() throws IOException




二、OutputStream

public abstract class OutputStream extends Object implements Closeable, Flushable
Since:
JDK1.0

 public abstract void write(int b) throws IOException;
写入一个字节
 public void write(byte b[]) throws IOException
把b[ ]数组里的字节写到输出流中
public void write(byte b[], int off, int len) throws IOException
把b[ ]数组里的字节从off位置循环输入len个字节到输入流中
 public void flush() throws IOException
public void close() throws IOException


0 0