电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

Java BufferedReader相關(guān)源碼實例分析

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

1、案例代碼

假設(shè)b.txt存儲了abcdegfhijk

public static void main(String[] args) throws IOException { //字符緩沖流 BufferedReader bufferedReader=new BufferedReader(new FileReader(new File('H:ioTextb.txt')),8); //存儲讀取的數(shù)據(jù) char[] charsRead=new char[5]; //讀取數(shù)據(jù) 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[];//緩沖區(qū) private int nChars, nextChar;//nChars緩沖區(qū)可讀字符數(shù),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;//緩沖區(qū)默認(rèn)大小 private static int defaultExpectedLineLength = 80; //案例調(diào)用的構(gòu)造方法 public BufferedReader(Reader in, int sz) { //調(diào)用父類構(gòu)造 super(in); //判斷緩沖區(qū)大小是否正常 if (sz <= 0) throw new IllegalArgumentException('Buffer size <= 0'); //用戶傳入的字符流 this.in = in; //給緩沖區(qū)指定空間大小(案例指定為8) cb = new char[sz]; //緩沖區(qū)可讀字符數(shù)和下一個字符位置初始化為0 nextChar = nChars = 0; } //讀取數(shù)據(jù) 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; } //調(diào)用read1方法進(jìn)行讀取(真正讀取數(shù)據(jù)的方法是read1方法) int n = read1(cbuf, off, len); if (n <= 0) return n; //將之前沒處理完的數(shù)據(jù)復(fù)制到自定以數(shù)組charsRead再次調(diào)用read1方法讀取 while ((n < len) && in.ready()) {int n1 = read1(cbuf, off + n, len - n);if (n1 <= 0) break;n += n1; } return n; } } //cbuf用戶自定義數(shù)組(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); } //刷新緩沖區(qū),先往下找到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; } } //執(zhí)行完fill方法到這里,(len=5,nChars - nextChar=8-0)->n=5 int n = Math.min(len, nChars - nextChar); //將緩沖區(qū)cb從nextChar開始復(fù)制n=5個字符到自定義數(shù)組 System.arraycopy(cb, nextChar, cbuf, off, n); //nextChar=5 nextChar += n; //n=5 return n; } //刷新緩沖區(qū)方法 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用戶自定義數(shù)組(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); //將緩沖區(qū)cb從nextChar=5開始復(fù)制n=3個字符到自定義數(shù)組 System.arraycopy(cb, nextChar, cbuf, off, n); //nextChar=5+3=8 nextChar += n; //n=8 return n; }

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

c、第三次讀取

//cbuf用戶自定義數(shù)組(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); } //刷新緩沖區(qū),先往下找到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; } } //執(zhí)行完fill方法到這里,(len=2,nChars - nextChar=8-0)->n=2 int n = Math.min(len, nChars - nextChar); //將緩沖區(qū)cb從nextChar=0開始復(fù)制n=2個字符到自定義數(shù)組 System.arraycopy(cb, nextChar, cbuf, off, n); //nextChar=5+3=8 nextChar += n; //n=8 return n; } //刷新緩沖區(qū)方法 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、源碼執(zhí)行過程圖解

Java BufferedReader相關(guān)源碼實例分析

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 广西绿桂涂料--承接隔热涂料、隔音涂料、真石漆、多彩仿石漆等涂料工程双包施工 | 逗网红-抖音网红-快手网红-各大平台网红物品导航 | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | 德国GMN轴承,GMN角接触球轴承,GMN单向轴承,GMN油封,GMN非接触式密封 | 衡阳耐适防护科技有限公司——威仕盾焊接防护用品官网/焊工手套/焊接防护服/皮革防护手套 | 江西自考网 | IIS7站长之家-站长工具-爱网站请使用IIS7站长综合查询工具,中国站长【WWW.IIS7.COM】 | 一点车讯-汽车网站,每天一点最新车讯! | 铝合金线槽_铝型材加工_空调挡水板厂家-江阴炜福金属制品有限公司 | 冷柜风机-冰柜电机-罩极电机-外转子风机-EC直流电机厂家-杭州金久电器有限公司 | 旋振筛_不锈钢旋振筛_气旋筛_旋振筛厂家—新乡市大汉振动机械有限公司 | 祝融环境-地源热泵多恒系统高新技术企业,舒适生活环境缔造者! | KBX-220倾斜开关|KBW-220P/L跑偏开关|拉绳开关|DHJY-I隔爆打滑开关|溜槽堵塞开关|欠速开关|声光报警器-山东卓信有限公司 | 电机铸铝配件_汽车压铸铝合金件_发动机压铸件_青岛颖圣赫机械有限公司 | 全自动变压器变比组别测试仪-手持式直流电阻测试仪-上海来扬电气 | 面粉仓_储酒罐_不锈钢储酒罐厂家-泰安鑫佳机械制造有限公司 | 河北码上网络科技|邯郸小程序开发|邯郸微信开发|邯郸网站建设 | 隔离变压器-伺服变压器--输入输出电抗器-深圳市德而沃电气有限公司 | HDPE土工膜,复合土工膜,防渗膜价格,土工膜厂家-山东新路通工程材料有限公司 | 深圳市八百通智能技术有限公司官方网站| 全自动端子机|刺破式端子压接机|全自动双头沾锡机|全自动插胶壳端子机-东莞市傅氏兄弟机械设备有限公司 | 山东限矩型液力偶合器_液力耦合器易熔塞厂家-淄博市汇川源机械厂 | 建大仁科-温湿度变送器|温湿度传感器|温湿度记录仪_厂家_价格-山东仁科 | 电镀整流器_微弧氧化电源_高频电解电源_微弧氧化设备厂家_深圳开瑞节能 | 999范文网_优质范文下载写作帮手 | 抖音短视频运营_企业网站建设_网络推广_全网自媒体营销-东莞市凌天信息科技有限公司 | 工业rfid读写器_RFID工业读写器_工业rfid设备厂商-ANDEAWELL | 好看的韩国漫画_韩漫在线免费阅读-汗汗漫画 | 精密模具制造,注塑加工,吹塑和吹瓶加工,EPS泡沫包装生产 - 济南兴田塑胶有限公司 | 二手Sciex液质联用仪-岛津气质联用仪-二手安捷伦气质联用仪-上海隐智科学仪器有限公司 | 天津电机维修|水泵维修-天津晟佳机电设备有限公司 | 厚壁钢管-厚壁无缝钢管-小口径厚壁钢管-大口径厚壁钢管 - 聊城宽达钢管有限公司 | 阿米巴企业经营-阿米巴咨询管理-阿米巴企业培训-广东键锋企业管理咨询有限公司 | 废旧物资回收公司_广州废旧设备回收_报废设备物资回收-益美工厂设备回收公司 | 苗木价格-苗木批发-沭阳苗木基地-沭阳花木-长之鸿园林苗木场 | 自清洗过滤器,浅层砂过滤器,叠片过滤器厂家-新乡市宇清净化 | 行星齿轮减速机,减速机厂家,山东减速机-淄博兴江机械制造 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 南京种植牙医院【官方挂号】_南京治疗种植牙医院那个好_南京看种植牙哪里好_南京茀莱堡口腔医院 尼龙PA610树脂,尼龙PA612树脂,尼龙PA1010树脂,透明尼龙-谷骐科技【官网】 | 电动打包机_气动打包机_钢带捆扎机_废纸打包机_手动捆扎机 | 真空粉体取样阀,电动楔式闸阀,电动针型阀-耐苛尔(上海)自动化仪表有限公司 |