• TextFile 类的创写


    TextFile 作为自写的方法,继承自List<String>。像统计文本中出现的哪些单词,不重复等等,适合用Set集合完成统计。

     1 class TextFile extends ArrayList<String>{
     2         public static String read(String filename){
     3             StringBuilder sb=new StringBuilder();
     4             try{
     5                 BufferedReader in=new BufferedReader(new FileReader(
     6                         new File(filename).getAbsoluteFile()));
     7                     try{
     8                         String s;
     9                         while((s=in.readLine())!=null){
    10                             sb.append(s);
    11                             sb.append("
    ");
    12                             }
    13                         }finally{
    14                 in.close();
    15                 }
    16             }catch(IOException e){
    17                 throw new RuntimeException(e);
    18             }
    19             return sb.toString();
    20     }
    21     public TextFile(String filename,String splitter){
    22         super(Arrays.asList(read(filename).split(splitter)));
    23         if(get(0).equals("")) remove(0);
    24     }
    25     public TextFile(String filename){
    26         this(filename,"
    ");
    27     }
    28     public void write(String filename){
    29         try{
    30             PrintWriter out=new PrintWriter(new File
    31                     (filename).getAbsoluteFile());
    32             try{
    33                 for(String item : this) out.println(item);
    34             }finally{
    35                 out.close();
    36                 }
    37             }catch(IOException e){
    38                 throw new RuntimeException(e);
    39                 }
    40             
    41     }
    42     public static void write(String filename,String text){
    43     // 其中filename指明要写入的文件名,text指明写入的字符串内容
    44         try{
    45             FileWriter fwriter=new FileWriter(new File
    46                     (filename).getAbsoluteFile());
    47             BufferedWriter out=new BufferedWriter(fwriter);
    48             String []tx=text.split("
    ");
    49             try{
    50                 for(int i=0;i<tx.length;i++)
    51                 {
    52                     out.write(tx[i]);
    53                     out.newLine();
    54                 }
    55             }finally{
    56                 out.flush();
    57                 out.close();
    58                 }
    59             }catch(IOException e){
    60                 throw new RuntimeException(e);
    61                 
    62             }
    63             
    64     }
    65 }
        public static void main(String[] args) {
            Set<String> words = new TreeSet<String>(
                new TextFile("StatckTest.java","\W+"));
            System.out.print(words);    
            System.out.println(words.size());
            
            Set<String> words2 = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
            words2.addAll(words);
            System.out.print(words2);
            System.err.println(words2.size());
            
            //这里有什么不同的?
            
        }
  • 相关阅读:
    命令基础
    绑定在表单验证上的应用
    绑定和绑定的各种使用场景
    双向数据绑定
    事件
    委托应用及泛型委托和多播委托
    委托
    LINQ
    反射重要属性方法
    反射基本内容
  • 原文地址:https://www.cnblogs.com/FakerWang/p/fffakerw.html
Copyright © 2020-2023  润新知