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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© _Water 中级黑马   /  2014-4-22 01:43  /  504 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.io.*;
  2. class Test
  3. {
  4.         public static void main(String[] args)throws IOException
  5.         {
  6.                 FileReader fr =new FileReader("buf.txt");

  7.                 MyBufferedReader mybufr =new MyBufferedReader(fr);

  8.                 String line =null;
  9.                 while((line=mybufr.myReadLine())!=null)
  10.                 {
  11.                         sop(line);
  12.                 }

  13.                 mybufr.myClose();
  14.         }
  15.        
  16.        

  17.         /**
  18.         通过缓冲区技术赋值一个。java文件
  19.         */
  20.         public static void copyTextfile()
  21.         {
  22.                 BufferedReader bufr =null;
  23.                 BufferedWriter bufw =null;
  24.                 try
  25.                 {
  26.                         bufr =new BufferedReader(new FileReader("Test.java"));
  27.                         bufw =new BufferedWriter(new FileWriter("Testcopy2.java"));

  28.                         String line =null;
  29.                         while((line=bufr.readLine())!=null)
  30.                         {
  31.                                 bufw.write(line);
  32.                                 bufw.newLine();
  33.                                 bufw.flush();
  34.                         }
  35.                 }
  36.                 catch (IOException e)
  37.                 {
  38.                         throw new RuntimeException("读写失败");
  39.                 }

  40.                 finally
  41.                 {
  42.                         try
  43.                         {
  44.                                 if (bufr!=null)
  45.                                         bufr.close();
  46.                         }
  47.                         catch (IOException e)
  48.                         {
  49.                                 throw new RuntimeException ("读取失败");
  50.                         }

  51.                         try
  52.                         {
  53.                                 if (bufw!=null)
  54.                                 {
  55.                                         bufw.close();
  56.                                 }
  57.                         }
  58.                         catch (IOException e)
  59.                         {
  60.                                 throw new RuntimeException("写入失败");
  61.                         }
  62.                 }

  63.         }
  64.         public static void bufferedReader()throws IOException
  65.         {
  66.                 FileReader fr =new FileReader("buf.txt");

  67.                 BufferedReader bufr=new BufferedReader(fr);

  68.                 String line =null;
  69.                 while((line=bufr.readLine())!=null)
  70.                         sop(line);
  71.         }

  72.         public static void sop(Object obj)
  73.         {
  74.                 System.out.println(obj);
  75.         }
  76. }



  77. ///////////////////////////////////////////////////////
  78. /**
  79.         自定义缓冲区并模仿一个和readLIne一致的方法
  80.         */
  81. class MyBufferedReader
  82. {
  83.         private FileReader r;
  84.         MyBufferedReader(FileReader r)
  85.         {
  86.                 this.r =r;
  87.         }
  88.         public String myReadLine() throws IOException
  89.         {
  90.                 //定义一个临时容器StringBuffer

  91.                 StringBuffer sb =new StringBuffer();
  92.                
  93.                 int ch =0;
  94.                 while((ch=r.read())!=-1)
  95.                 {
  96.                         if (ch=='\r')
  97.                                 continue;
  98.                         if (ch=='\n')
  99.                                 return sb.toString();
  100.                         else sb.append((char)ch);

  101.                        
  102.                 }
  103.                 if (sb.length()!=0)
  104.                         return sb.toString();
  105.                 return null;
  106.         }

  107.         public void myClose() throws IOException
  108.         {
  109.                 r.close();
  110.         }
  111. }
复制代码

1 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马