InputStream InputStreamReader BufferedReader详解

基本概念

Java有字节流和字符流两种:

字节流:InputStream/OutputStream

字符流:Reader/Writer

字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串,而字节流处理单元为1个字节,操作字节和字节数组。所以字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的,所以它对多国语言支持性比较好!如果是音频文件、图片、歌曲,就用字节流好点,如果是关系到中文(文本)的,用字符流好点.

处理字节流

InputStream 是字节输入流的所有类的超类,一般我们使用它的子类,如FileInputStream等.
OutputStream是字节输出流的所有类的超类,一般我们使用它的子类,如FileOutputStream等.

处理字符流

Reader和Writer是所有字符流类的的抽象基类,用于简化对字符串的输入输出编程,即用于读写文本数据

字节流和字符流的相互转换

1.从字节流到字符流

InputStreamReader 是字节流通向字符流的桥梁,它将字节流转换为字符流.
OutputStreamWriter是字符流通向字节流的桥梁,它将字符流转换为字节流.

2.从字符流到字节流

可以从字符流中获取char[]数组,转换为String,然后调用String的API函数getBytes() 获取到byte[],然后就可以通过ByteArrayInputStream、ByteArrayOutputStream来实现到字节流的转换。

BufferedReader BufferedWriter

BufferedReader 由Reader类扩展而来,提供通用的缓冲方式文本读取,readLine读取一个文本行,从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。

实际上,BufferedWriter是对OutputStreamWriter的封装,BufferedReader是对InputStreamReader的封装,封装格式:

BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader in= new BufferedReader(new InputStreamReader(System.in);

我们从BufferedReader源码中可以看出:

public class BufferedReader extends Reader {
    private Reader in;
    private char cb[];
    ...
    private static int defaultCharBufferSize = 8192;
    
    public BufferedReader(Reader in, int sz) {
	    super(in);
	    if (sz <= 0)
	        throw new IllegalArgumentException("Buffer size <= 0");
	    this.in = in; /* 对Reader进行封装 */
	    cb = new char[sz];
	    nextChar = nChars = 0;
    }
    public BufferedReader(Reader in) {
	    this(in, defaultCharBufferSize);
    }
    
    /**
     * Fills the input buffer, taking the mark into account if it is valid.
     */
    private void fill() throws IOException {
	      int dst;
	      if (markedChar <= UNMARKED) {
	          /* No mark */
	          dst = 0;
	      } else {
	          /* Marked */
	          int delta = nextChar - markedChar;
	          if (delta >= readAheadLimit) {
		            /* Gone past read-ahead limit: Invalidate mark */
		            markedChar = INVALIDATED;
		            readAheadLimit = 0;
		            dst = 0;
	          } else {
		            if (readAheadLimit <= cb.length) {
		                /* Shuffle in the current buffer */
		                System.arraycopy(cb, markedChar, cb, 0, delta);
		                markedChar = 0;
		                dst = delta;
		            } else {
		                /* Reallocate buffer to accommodate read-ahead limit */
		                char ncb[] = new char[readAheadLimit];
		                System.arraycopy(cb, markedChar, ncb, 0, delta);
		                cb = ncb;
		                markedChar = 0;
		                dst = delta;
		            }
                nextChar = nChars = delta;
	          }
	      }

	      int n;
	      do {
	          /* 从in中读取字符到缓冲区cb中 */
	          n = in.read(cb, dst, cb.length - dst);
	      } while (n == 0);
	      if (n > 0) {
	          nChars = dst + n;
	          nextChar = dst;
	      }
    }
    
    /**
     * Reads a single character.
     *
     * @return The character read, as an integer in the range
     *         0 to 65535 (0x00-0xffff), or -1 if the
     *         end of the stream has been reached
     * @exception  IOException  If an I/O error occurs
     */
     public int read() throws IOException {
	     synchronized (lock) {
	       ensureOpen();
	       for (;;) {
		         if (nextChar >= nChars) {
		             fill(); /* 从字符流in中读取字符填充cb缓冲区 */
		             if (nextChar >= nChars)
			               return -1;
		         }
		         if (skipLF) {
		             skipLF = false;
		             if (cb[nextChar] == '\n') {
			               nextChar++;
			               continue;
		             }
		         }
		         return cb[nextChar++]; /* 从缓冲区中读取字符 */
	       }
	     }
     }
     ...
}
BufferedReader的最大特点就是缓冲区的设置。通常Reader 所作的每个读取请求都会导致对底层字符或字节流进行相应的读取请求,如果没有缓冲,则每次调用 read() 或 readLine() 都会导致从文件中读取字节,并将其转换为字符后返回,而这是极其低效的。 
使用BufferedReader可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。 

因此,建议用 BufferedReader 包装所有其 read() 操作可能开销很高的 Reader(如 FileReader 和InputStreamReader)。



你可能感兴趣的:(java)