单词频率

第一次学习如何从文件中读取,在网上学习的也差不多了,然后就接着学习如何导入到文件中,总结出来就是自己看不懂代码,这次遇到了极大的困难,本来C和C++文件这一部分就没有学,Java于是就更突出了,然后统频率也是困难重重,我大概计算了下时间,用了4个小时竟然什么都没有做出来,这次真的是听郁闷的,以后还是要多多学习,现在Java的代码还是有很多都是看不懂,真的是受打击了。

下面是这次写的代码:

  1 package DaoRu;
  2 import java.io.*;
  3 import java.util.HashMap;
  4 import java.util.regex.Matcher;
  5 import java.util.regex.Pattern;
  6 public class DaoRu {
  7     public static void main(String[] args) {         
  8        //读取D盘的file1文件
  9        File file = new File("D://file1.txt");
 10        BufferedInputStream bis = null;
 11        FileInputStream  fis= null;
 12        try
 13        {
 14            //第一步 通过文件路径来创建文件实例
 15            fis = new FileInputStream(file);
 16            /*把FileInputStream实例 传递到 BufferedInputStream
 17              目的是能快速读取文件
 18             */
 19            bis = new BufferedInputStream(fis);
 20            /*available检查是不是读到了文件末尾 */
 21            while( bis.available() > 0 ){                 
 22                System.out.print((char)bis.read());
 23            }
 24         }catch(FileNotFoundException fnfe)
 25          {
 26              System.out.println("文件不存在" + fnfe);
 27          }
 28          catch(IOException ioe)
 29          {
 30              System.out.println("I/O 错误: " + ioe); 
 31          }
 32          finally
 33          {
 34              try{
 35                 if(bis != null && fis!=null)
 36                 {
 37                       fis.close();
 38                    bis.close();
 39                 }      
 40               }catch(IOException ioe)
 41                {
 42                    System.out.println("关闭InputStream句柄错误: " + ioe);
 43                }         
 44          }
 45     }
 46 }
 47 package DaoChu;
 48 import java.io.FileNotFoundException;
 49 import java.io.FileWriter;
 50 import java.io.IOException;
 51  
 52 public class ShuRu {
 53     public static void rwFile(){
 54         FileWriter fw = null;
 55         try {
 56             fw = new FileWriter("D:\\file2.txt", true);
 57                 fw.write("aaaaa");//这里向文件中输入结果123
 58                 fw.flush();
 59         } catch (FileNotFoundException e) {
 60             e.printStackTrace();
 61         } catch (IOException e) {
 62             e.printStackTrace();
 63         } finally {
 64             if (fw != null) {
 65                 try {
 66                     fw.close();
 67                 } catch (IOException e) {
 68                     // TODO Auto-generated catch block
 69                     e.printStackTrace();
 70                 }
 71             }
 72         }
 73     }
 74     public static void main(String[] args) {
 75         rwFile();
 76     }
 77 }
 78 package Dictionary;
 79  import java.util.*;
 80  import java.util.regex.*;
 81  public class Dictionary{
 82      public static void main(String[] args){
 83          String words = "Look buddy, U got work hard and put yourself in your java, Once you learned the heart of the java, I can guarantee that you win.";
 84          String reg = "[a-zA-Z]+";
 85          Pattern p = Pattern.compile(reg);
 86          Matcher m = p.matcher(words);
 87          HashMap<String,Integer> map = new HashMap<String, Integer>();
 88          int count = 0;
 89          while(m.find()){
 90                  count++;
 91                  String w = m.group();
 92              if(null == map.get(w)){
 93                  map.put(w, 1);
 94              }else{
 95                  int x = map.get(w);
 96                  map.put(w, x + 1);
 97              }
 98          }
 99          System.out.println(count);
100          System.out.println(map);
101      }
102  }

 

 

 
posted on 2018-10-11 20:37  打篮球的Curry  阅读(200)  评论(0编辑  收藏  举报