黑马程序员——自定义类模拟实现LineNumberReader类

来源:互联网 发布:windows安装snmp 编辑:程序博客网 时间:2024/06/04 17:51

                                          ----------------------    android培训    java培训   期待与您交流!    ----------------------   
                 

import java.io.FileReader;import java.io.IOException;/** * 自定义类模拟实现LineNumberReader类 * @author wjw * */public class Hello{public static void main(String[] args)throws IOException  {MyBuffered m=new MyBuffered(new FileReader("g:\\java\\yes.txt"));String str=null;m.setCount(25);while(null!=(str=m.myReadLine())){System.out.println(m.getLineNumber()+":"+str);}}}class MyBuffered{private FileReader fr;private int count;public MyBuffered(FileReader fr){this.fr=fr;}public String myReadLine() throws IOException{count++;//读取一行,行数+1;int num=0;StringBuilder sb=new StringBuilder();while(-1!=(num=fr.read())){if(num=='\r'){continue;}else if(num=='\n'){return sb.toString();}else{sb.append((char)num);}}if(sb.length()>0){return sb.toString();}return null;}public int getLineNumber(){return count;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}}

                                          ----------------------    android培训    java培训   期待与您交流!    ----------------------   
                         详细请查看      http://edu.csdn.net/heima


原创粉丝点击