A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 不怕黑人 中级黑马   /  2015-7-26 21:26  /  247 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package fuxi2;

  2. import java.io.FileReader;
  3. import java.io.IOException;

  4. /**
  5. * 需求:自定义个MyLineNumberReader装饰类,实现LineNumberReader功能。
  6. *
  7. *@author XiaLei
  8. */
  9. public class Day19Test2 {

  10.         public static void main(String[] args) {

  11.                 MyLineNumberReader lnr = null;
  12.                 try{
  13.                         lnr = new MyLineNumberReader(new FileReader("d:\\StringTest.txt"));
  14.                         String line = null;
  15.                         lnr.mySetLineNumber(100);
  16.                         while((line=lnr.myReadLine())!=null){
  17.                                 System.out.println(lnr.myGetLineNumber()+":"+line);
  18.                         }
  19.                 }
  20.                 catch(IOException e){
  21.                         throw new RuntimeException("读取失败");
  22.                 }
  23.                 finally{
  24.                         try{
  25.                                 if(lnr!=null){
  26.                                         lnr.myClose();
  27.                                 }
  28.                         }
  29.                         catch(IOException e){
  30.                                 throw new RuntimeException("关流失败");
  31.                         }
  32.                 }
  33.         }

  34. }
  35. class MyLineNumberReader{//这个类跟前面自定义MyBufferedReader装饰类,有很多相似功能,可以继承MyBufferedReader类,简化书写。
  36.         private FileReader fr;
  37.         private int count;
  38.         private int x;
  39.         MyLineNumberReader(FileReader fr){
  40.                 this.fr = fr;
  41.         }
  42.         public String myReadLine() throws IOException{//根据readLine读取到换行符就返回之前读到的内容这一原理,设计自己的读取方式。
  43.                
  44.                 StringBuilder sb = new StringBuilder();//建立容器,将读到的内容存入。
  45.                 int num = 0;
  46.                 while((num=fr.read())!=0){
  47.                         if(num=='\n')
  48.                                 continue;
  49.                         if(num=='\r')
  50.                                 return sb.toString();
  51.                         else
  52.                                 sb.append((char)num);
  53.                 }
  54.                 if(sb.length()!=0)
  55.                         return sb.toString();
  56.                 return null;
  57.         }
  58.         public int mySetLineNumber(int x){
  59.                 return this.x=x;
  60.         }
  61.         public int myGetLineNumber(){
  62.                 if(count==0)
  63.                         count = x;
  64.                 return 1+count++;
  65.         }
  66.         public void myClose() throws IOException{//建立自己的关流动作。
  67.                 fr.close();
  68.         }
  69. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马