- 论坛徽章:
- 0
|
Java统计文档中英文单词个数
Java代码- 1.public class CountWords {
- 2. public static void main(String[] args) {
- 3. BufferedReader br = null;
- 4. try {
- 5. br = new BufferedReader(new FileReader("english.txt"));
- 6. } catch (FileNotFoundException e) {
- 7. e.printStackTrace();
- 8. }
- 9. StringBuffer sb = new StringBuffer();
- 10. String line = null;
- 11. try {
- 12. while((line = br.readLine()) != null) {
- 13. sb = sb.append(line);
- 14. }
- 15. } catch (IOException e) {
- 16. e.printStackTrace();
- 17. }
- 18. try {
- 19. br.close();
- 20. } catch (IOException e1) {
- 21. e1.printStackTrace();
- 22. }
- 23.
- 24. Pattern pattern = Pattern.compile("[a-zA-Z']+");
- 25. Matcher matcher = pattern.matcher(sb);
- 26. Map<String, Integer> map = new HashMap<String, Integer>();
- 27. String word = "";
- 28. Integer num = null;
- 29. int total = 0;
- 30.
- 31. while(matcher.find()) {
- 32. word = matcher.group();
- 33. total ++;
- 34. if(map.containsKey(word)) {
- 35. num = map.get(word);
- 36. num += 1;
- 37. } else {
- 38. num = 1;
- 39. }
- 40. map.put(word, num);
- 41. }
- 42.
- 43. PrintWriter pw = null;
- 44. try {
- 45. pw = new PrintWriter(new FileWriter("result.txt"), true);
- 46. } catch (IOException e) {
- 47. e.printStackTrace();
- 48. }
- 49. Iterator<String> iterator = map.keySet().iterator();
- 50. while(iterator.hasNext()) {
- 51. String tmp = iterator.next();
- 52. pw.println(tmp + " : " + map.get(tmp));
- 53. }
- 54. pw.println("total words : " + total);
- 55. pw.println("different words : " + map.size());
- 56. pw.close();
- 57.
- 58. }
- 59.}
复制代码 |
|