?? ex1.java
字號:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package chenchao4;import java.io.*;import java.util.*;/** * * @author williechen */public class Ex1 { private String fileName = new String(); private ArrayList<String> lines = new ArrayList<String>(); private HashMap<String, Integer> wordList = new HashMap<String, Integer>(); private void loadFileIntoLines() { try { File file = new File(fileName); FileReader fReader = new FileReader(file); Scanner sc = new Scanner(fReader); while (sc.hasNextLine()) { lines.add(sc.nextLine()); } sc.close(); fReader.close(); } catch (IOException ioe) { System.out.println(ioe.getStackTrace()); } } private void scanALine(String s) { String patten = " " + ',' + ';' + '-' + '.' + '。'+ '、' + ','; StringTokenizer st = new StringTokenizer(s, patten); while (st.hasMoreTokens()) { String word = st.nextToken(); int count = 1; if (wordList.containsKey(word)) { count = wordList.get(word); count++; wordList.remove(word); } wordList.put(word, count); } } private void doStatistics() { for(String s : lines) scanALine(s); int wordSum = 0; Collection<Integer> countList = wordList.values(); for(int i : countList) wordSum += i; int max = Collections.max(countList); System.out.println("Lines counts is :" + lines.size()); System.out.println("The Total Words Counts is:" + wordSum); System.out.println("The words used max counts is :" + max); System.out.println("Below is the WordList:"); System.out.println(wordList); } public Ex1(String fileName) { this.fileName = fileName; } public static void main(String[] args) { Ex1 t = new Ex1("D:\\cloze.txt"); t.loadFileIntoLines(); t.doStatistics(); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -