色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Java BufferedReader相關源碼實例分析

瀏覽:5日期:2022-08-21 17:25:54

1、案例代碼

假設b.txt存儲了abcdegfhijk

public static void main(String[] args) throws IOException { //字符緩沖流 BufferedReader bufferedReader=new BufferedReader(new FileReader(new File('H:ioTextb.txt')),8); //存儲讀取的數據 char[] charsRead=new char[5]; //讀取數據 bufferedReader.read(charsRead); //遍歷并輸出charsRead for (char c:charsRead){ System.out.println(c); } }

2、通過源碼(部分)分析案例

a、第一次讀取

public class BufferedReader extends Reader { private Reader in;//字符流 private char cb[];//緩沖區 private int nChars, nextChar;//nChars緩沖區可讀字符數,nextChar下一個字符位置 private static final int INVALIDATED = -2; private static final int UNMARKED = -1; private int markedChar = UNMARKED; private int readAheadLimit = 0; private boolean skipLF = false; private boolean markedSkipLF = false; private static int defaultCharBufferSize = 8192;//緩沖區默認大小 private static int defaultExpectedLineLength = 80; //案例調用的構造方法 public BufferedReader(Reader in, int sz) { //調用父類構造 super(in); //判斷緩沖區大小是否正常 if (sz <= 0) throw new IllegalArgumentException('Buffer size <= 0'); //用戶傳入的字符流 this.in = in; //給緩沖區指定空間大小(案例指定為8) cb = new char[sz]; //緩沖區可讀字符數和下一個字符位置初始化為0 nextChar = nChars = 0; } //讀取數據 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; } //調用read1方法進行讀取(真正讀取數據的方法是read1方法) int n = read1(cbuf, off, len); if (n <= 0) return n; //將之前沒處理完的數據復制到自定以數組charsRead再次調用read1方法讀取 while ((n < len) && in.ready()) {int n1 = read1(cbuf, off + n, len - n);if (n1 <= 0) break;n += n1; } return n; } } //cbuf用戶自定義數組(charsRead),off=0,len=5 private int read1(char[] cbuf, int off, int len) throws IOException { if (nextChar >= nChars) {//第一次讀nextChar、nChars都為0,滿足條件 if (len >= cb.length && markedChar <= UNMARKED && !skipLF) {return in.read(cbuf, off, len); } //刷新緩沖區,先往下找到fill方法源碼分析 fill(); } if (nextChar >= nChars) return -1; if (skipLF) { skipLF = false; if (cb[nextChar] == ’n’) {nextChar++;if (nextChar >= nChars) fill();if (nextChar >= nChars) return -1; } } //執行完fill方法到這里,(len=5,nChars - nextChar=8-0)->n=5 int n = Math.min(len, nChars - nextChar); //將緩沖區cb從nextChar開始復制n=5個字符到自定義數組 System.arraycopy(cb, nextChar, cbuf, off, n); //nextChar=5 nextChar += n; //n=5 return n; } //刷新緩沖區方法 private void fill() throws IOException { int dst; if (markedChar <= UNMARKED) {//markedChar初始值為UNMARKED,滿足條件 /* No mark */ dst = 0;//初始化dst } 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 { //dst=0,cb.length - dst=8-0->n=8 n = in.read(cb, dst, cb.length - dst); } while (n == 0); if (n > 0) {//滿足條件 //nChars=8 nChars = dst + n; //nextChar=0 nextChar = dst; } } }

第一次讀取后charsRead存儲了五個字符:abcde

b、第二次讀取

//cbuf用戶自定義數組(charsRead),off=0,len=5 private int read1(char[] cbuf, int off, int len) throws IOException { if (nextChar >= nChars) {//第二次讀nextChar=5、nChars=8,不滿足條件 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; } } //跳過if直接到這里,len=5,nChars - nextChar=8-5=3->n=3 int n = Math.min(len, nChars - nextChar); //將緩沖區cb從nextChar=5開始復制n=3個字符到自定義數組 System.arraycopy(cb, nextChar, cbuf, off, n); //nextChar=5+3=8 nextChar += n; //n=8 return n; }

第二次讀取只讀了三個字符把charsRead五個字符的前三個覆蓋:fghde

c、第三次讀取

//cbuf用戶自定義數組(charsRead),off=0,len=5 private int read1(char[] cbuf, int off, int len) throws IOException { if (nextChar >= nChars) {//第三次讀nextChar=8、nChars=8,滿足條件 if (len >= cb.length && markedChar <= UNMARKED && !skipLF) {return in.read(cbuf, off, len); } //刷新緩沖區,先往下找到fill方法源碼分析 fill(); } if (nextChar >= nChars) return -1; if (skipLF) { skipLF = false; if (cb[nextChar] == ’n’) {nextChar++;if (nextChar >= nChars) fill();if (nextChar >= nChars) return -1; } } //執行完fill方法到這里,(len=2,nChars - nextChar=8-0)->n=2 int n = Math.min(len, nChars - nextChar); //將緩沖區cb從nextChar=0開始復制n=2個字符到自定義數組 System.arraycopy(cb, nextChar, cbuf, off, n); //nextChar=5+3=8 nextChar += n; //n=8 return n; } //刷新緩沖區方法 private void fill() throws IOException { int dst; if (markedChar <= UNMARKED) {//markedChar初始值為UNMARKED,滿足條件 /* No mark */ dst = 0;//初始化dst } 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 { //dst=0,cb.length - dst=8-0->n=8 n = in.read(cb, dst, cb.length - dst); } while (n == 0); if (n > 0) {//滿足條件 //nChars=8 nChars = dst + n; //nextChar=0 nextChar = dst; } } }

第三次讀取了兩個字符到charsRead,把最后兩個字符覆蓋:fghijk

3、源碼執行過程圖解

Java BufferedReader相關源碼實例分析

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 欧美日韩精品在线播放 | 日韩不卡一级毛片免费 | 国产一区二区三区久久 | 婷婷的久久五月综合先锋影音 | 九九精品激情在线视频 | 亚洲国产高清视频 | 九九视频在线观看视频6偷拍 | 精品国产欧美一区二区 | 久草在线资源网站 | 日韩a一级欧美一级 | 国产日韩欧美视频在线 | 免费看a级肉片 | 国产成人综合自拍 | 在线观看毛片网站 | 国产aⅴ精品一区二区三区久久 | 亚洲久草 | 亚洲国产欧美91 | 国产日韩亚洲欧美 | 最新版天堂资源中文官网 | 亚洲高清自拍 | 大胆gogo999亚洲肉体艺术 | 另类自拍 | 无毛片| 毛片在线全部免费观看 | 欧美a极品极品欧美 | 国产毛片一区二区三区精品 | 手机看片1024久久精品你懂的 | 国产亚洲高清视频 | 国产综合精品一区二区 | 国产亚洲欧美视频 | 农村寡妇女人一级毛片 | 日本一级在线播放线观看免 | 一级欧美激情毛片 | 亚洲第一区精品日韩在线播放 | 成人看片在线观看免费 | 美女国产福利视频 | 在线视频 日韩 | 国产成人在线播放 | 欧美亚洲另类久久综合 | 成人欧美在线观看 | 特黄特黄|