[黑马程序员](第40天)一些平时刷的题

------- android培训java培训、期待与您交流! ----------
[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.io.FileReader;  
  4. import java.io.IOException;  
  5. import java.io.Reader;  
  6.   
  7. /** 
  8.  * 编写一个类,增强java.io.BufferedReader的ReadLine()方法,使之在读取某个文件时,能打印出行号。 
  9.  *   
  10.  *  就是一个模拟BufferedReadLine的程序 
  11.  *  定义一个计数器,定义Reader 
  12.  *  模拟ReadLine方法,当读取一行,计数器+1,遇到\r\n就表示应该换行 
  13.  *  关闭缓冲区 
  14.  * 
  15.  */  
  16. public class Test1   
  17. {  
  18.     public static void main(String[] args)  
  19.     {  
  20.           
  21.         MyLineNumReader mnr = null;  
  22.         try  
  23.         {  
  24.             mnr = new MyLineNumReader(new FileReader("Test.txt"));  
  25.               
  26.             String line = null;  
  27.               
  28.             while ((line = mnr.myReadLine()) != null)  
  29.             {  
  30.                 System.out.println(mnr.getCount()+":\t"+line);  
  31.             }  
  32.         }  
  33.         catch (IOException e)  
  34.         {  
  35.             System.out.println("读写错误");  
  36.         }  
  37.         finally  
  38.         {  
  39.             try  
  40.             {  
  41.                 if (mnr != null)  
  42.                     mnr.myClose();  
  43.             }  
  44.             catch (IOException e)  
  45.             {  
  46.                 System.out.println("流关闭错误");  
  47.             }  
  48.         }  
  49.     }  
  50. }  
  51.   
  52. //声明一个类,模拟BufferedReader方法  
  53. class MyLineNumReader  
  54. {  
  55.     private Reader r;  
  56.     private int count;  
  57.       
  58.     //构造函数,使可以被创建对象  
  59.     MyLineNumReader(Reader r)  
  60.     {  
  61.         this.r = r;  
  62.     }  
  63.       
  64.     //定义一个myReadLine方法,功能是读取一行数据  
  65.     public String myReadLine() throws IOException  
  66.     {         
  67.             StringBuffer sb = new StringBuffer ();  
  68.               
  69.             int num = 0;  
  70.             count ++;  
  71.             while ((num = r.read()) != -1)  
  72.             {  
  73.                 if (num == '\r')  
  74.                     continue;  
  75.                 if (num == '\n')  
  76.                     return sb.toString();  
  77.                 else  
  78.                     sb.append((char)num);  
  79.             }  
  80.             if (sb.length() != 0)  
  81.                 return sb.toString();  
  82.             return null;  
  83.     }  
  84.       
  85.     //必要的读取属性的方法  
  86.     public int getCount() {  
  87.         return count;  
  88.     }  
  89.       
  90.     必要的修改属性的方法  
  91.     public void setCount(int count) {  
  92.         this.count = count;  
  93.     }     
  94.       
  95.     //关流方法  
  96.     public void myClose() throws IOException  
  97.     {  
  98.         r.close();  
  99.     }  
  100. }  

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.FilenameFilter;  
  9. import java.io.IOException;  
  10.   
  11. /** 
  12.  * 编写程序,将指定目录下所有的.java文件拷贝到另一个目的中,将扩展名改为.txt。 
  13.  *  定义一个函数,函数功能是遍历当前目录所有非文件夹的文件 
  14.  *  把后缀是.java文件的文件拷贝到另一个文件夹中 
  15.  *  修改她们的后缀名为.txt,用String的replace方法。 
  16.  * 
  17.  */  
  18. public class Test3   
  19. {  
  20.     public static void main(String[] args)   
  21.     {  
  22.         File f = new File("F:\\java\\day20");  
  23.         copyFile(f);  
  24.     }  
  25.       
  26.     //函数功能:把一个文件夹下面的.java结尾的文件复制到copy文件夹下面并改名  
  27.     public static void copyFile(File f)  
  28.     {  
  29.         //创建一个copy文件加,复制后的文件会被装进来  
  30.         File copy = new File("copy");  
  31.         copy.mkdir();  
  32.   
  33.         //过滤。java文件  
  34.         File[] files = f.listFiles(new FilenameFilter()  
  35.         {  
  36.             public boolean accept(File f,String name)  
  37.             {  
  38.                 return name.endsWith(".java");  
  39.             }  
  40.         });  
  41.           
  42.         //复制文件并改名  
  43.         for (File fileName : files)  
  44.         {  
  45.             BufferedInputStream bis = null;  
  46.             BufferedOutputStream bos = null;  
  47.                   
  48.             try  
  49.             {  
  50.                 bis = new BufferedInputStream(new FileInputStream(fileName));  
  51.                 //把后缀名.java改成.txt  
  52.                 String newName = fileName.getName().replace(".java",".txt");  
  53.                   
  54.                 bos = new BufferedOutputStream(new FileOutputStream(new File(copy,newName)));  
  55.                   
  56.                 byte[] by = new byte[1024];  
  57.                 int num = 0;  
  58.                   
  59.                 while ((num = bis.read()) != -1)  
  60.                 {  
  61.                     bos.write(by,0,num);  
  62.                 }  
  63.             }  
  64.             catch(IOException e)  
  65.             {  
  66.                 throw new RuntimeException("读写出错");  
  67.             }  
  68.             finally  
  69.             {  
  70.                 try   
  71.                 {  
  72.                     if (bis != null)  
  73.                         bis.close();  
  74.                 }  
  75.                 catch(IOException e)  
  76.                 {  
  77.                     throw new RuntimeException("读取关闭出错");  
  78.                 }  
  79.                 try   
  80.                 {  
  81.                     if (bos != null)  
  82.                         bos.close();  
  83.                 }  
  84.                 catch(IOException e)  
  85.                 {  
  86.                     throw new RuntimeException("写入关闭出错");  
  87.                 }  
  88.             }  
  89.         }  
  90.     }  
  91. }  

[java]  view plain copy print ?
  1. package com.gotoheima;  
  2.   
  3. import java.lang.reflect.Method;  
  4. import java.util.ArrayList;  
  5.   
  6. /** 
  7.  *  
  8.  *  ArrayList list = new ArrayList(); 在这个泛型为Integer的ArrayList中存放一个String类型的对象。 
  9.  *   
  10.  *  这里考察的是反射,获取ArrayList的字节码对象 
  11.  *  用Method的方法获取add的字段 
  12.  *  绕过编译阶段去添加String类型对象 
  13.  * 
  14.  */  
  15. public class Test4   
  16. {  
  17.     public static void main(String[] args) throws Exception  
  18.     {  
  19.         ArrayList<Integer> list = new ArrayList<Integer>();  
  20.           
  21.         Method methodAdd = list.getClass().getMethod("add",Object.class);  
  22.           
  23.         methodAdd.invoke(list,"String");  
  24.           
  25.         System.out.println(list);  
  26.     }  
  27. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值