Java_io体系之BufferedWriter、BufferedReader简介、走进源码及示例――16(三)

2014-11-24 08:05:03 · 作者: · 浏览: 1
ade harmlessly. */ if (len >= cb.length && markedChar <= UNMARKED && !skipLF) { return in.read(cbuf, off, len); } fill(); } if (nextChar >= nChars) return -1; if (skipLF) { skipLF = false; if (cb[nextChar] == '\n') { nextChar++; if (nextChar >= nChars) fill(); if (nextChar >= nChars) return -1; } } int n = Math.min(len, nChars - nextChar); System.arraycopy(cb, nextChar, cbuf, off, n); nextChar += n; return n; } /** * 将in中len个字符读取到cbuf从下标off开始长度len中 */ public int read(char cbuf[], int off, int len) throws IOException { synchronized (lock) { ensureOpen(); if ((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return 0; } int n = read1(cbuf, off, len); if (n <= 0) return n; while ((n < len) && in.ready()) { int n1 = read1(cbuf, off + n, len - n); if (n1 <= 0) break; n += n1; } return n; } } /** * 从in中读取一行、是否忽略换行符 */ String readLine(boolean ignoreLF) throws IOException { StringBuffer s = null; int startChar; synchronized (lock) { ensureOpen(); boolean omitLF = ignoreLF || skipLF; bufferLoop: for (;;) { if (nextChar >= nChars) fill(); if (nextChar >= nChars) { /* EOF */ if (s != null && s.length() > 0) return s.toString(); else return null; } boolean eol = false; char c = 0; int i; /* Skip a leftover '\n', if necessary */ if (omitLF && (cb[nextChar] == '\n')) nextChar++; skipLF = false; omitLF = false; charLoop: for (i = nextChar; i < nChars; i++) { c = cb[i]; if ((c == '\n') || (c == '\r')) { eol = true; break charLoop; } } startChar = nextChar; nextChar = i; if (eol) { String str; if (s == null) { str = new String(cb, startChar, i - startChar); } else { s.append(cb, startChar, i - startChar); str = s.toString(); } nextChar++; if (c == '\r') { skipLF = true; } return str; } if (s == null) s = new StringBuffer(defaultExpectedLineLength); s.append(cb, startChar, i - startChar); } } } /** * 从in中读取一行、 */ public String readLine() throws IOException { return readLine(false); } /** * 丢弃in中n个字符 */ public long skip(long n) throws IOException { if (n < 0L) { throw new IllegalArgumentException("skip value is negative"); } synchronized (lock) { ensureOpen(); long r = n; while (r > 0) { if (nextChar >= nChars) fill(); if (nextChar >= nChars) /* EOF */ break; if (skipLF) { skipLF = false; if (cb[nextChar] == '\n') { nextChar++; } } long d = nChars - nextChar; if (r <= d) { nextChar += r; r = 0; break; } else { r -= d; nextChar = nChars; } } return n - r; } } /** * 判断cb中是否为空、或者底层in中是否有可读字符。 */ public boolean ready() throws IOException { synchronized (lock) { ensureOpen(); /* * If newline needs to be skipped and the next char to be read * is a newline character, then just skip it right away. */ if (skipLF) { /* Note that in.ready() will return true if and only if the next * read on the stream will not block. */ if (nextChar >= nChars && in.ready()) { fill(); } if (nextChar < nChars) { if (cb[nextChar] == '\n') nextChar++; skipLF = false; } } return (nextChar < nChars) || in.ready(); } } /** * 判断此流是否支持标记 */ public boolean markSupported() { return true; } /** * 标记此流此时的位置、当调用reset方法失效前最多允许读取readAheadLimit个字符。 */ public void mark(int readAheadLimit) throws IOException { if (readAheadLimit < 0) { throw new IllegalArgumentException("Read-ahead limit < 0"); } synchronized (lock) { ensureOpen(); this.readAheadLimit = readAheadLimit; markedChar = nextChar; markedSkipLF = skipLF; } } /** * 重置in被最后一次mark的位置。即下一个字符从被最后一次mark的位置开始读取。 */ public void reset() throws IOException { synchronized (lock) { ensureOpen(); if (markedChar < 0) throw new IOException((markedChar == INVALIDATED) "Mark invalid" : "Stream not marked"); nextChar = markedChar; skipLF = markedSkipLF; } } //关闭此流、释放与此流有关的所有资源 public void close() throws IOException { synchronized (lock) { if (in == null) return; in.close(); in = null; cb = null; } } }

4、实例演示:


package com.chy.io.original.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterAndBufferedReaderTest {
	/**
	 * 这里对这两个类的测试比较简单、就是对文件字符流进行包装、实现文件拷贝
	 * 有兴趣的可以测试一下效率、、偷个懒、、可无视
	 */
	public static void main(String[] args) throws IOException{
		File resouceFile = new File("D:\\test.txt");
		File targetFile = new File("E:\\copyOftest.txt");
		
		BufferedReader br = new BufferedReader(new FileReader(resouceFile));
		BufferedWriter bw = new BufferedWriter(new FileWriter(targetFile));
		
		char[] cbuf = new char[1024];
		int n = 0;
		while((n = br.read(cbuf)) != -1){
			bw.write(cbuf, 0, n);
		}
		//不要忘记刷新和关闭流、否则一方面资源没有及时释放、另一方面有可能照成数据丢失
		br.close();
		bw.flush();
		bw.close();
	}
}

总结:


对于BufferedReader、BufferedWriter、本质就是为底层字符输入输出流添加缓冲功能、先将底层流中的要读取或者要写入的数据先以一次读取一组的形式来讲数据读取或者写入到buffer中、再对buffer进行操作、这样不但效率、还能节省资源。最后、在程序中、出于效率的考虑、也应为低级流使用这两个类进行装饰一下、而不是直接拿着流直接上、觉得能实现就行。


更多IO内容:java_io 体系之目录