-
Notifications
You must be signed in to change notification settings - Fork 1
/
shuliang.java
67 lines (65 loc) · 1.57 KB
/
shuliang.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package a.a;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class tongjidancishumu{
private static Map<String, Integer> words = new HashMap<String,Integer>();
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String src = "E:/Word.txt";
String wenzhang=ReadSrc(src);
words = Count(wenzhang);
Iterator<String> it = words.keySet().iterator();
while(it.hasNext()){
Object key = it.next();
System.out.println(key);
System.out.println(words.get(key));
}
}
public static String ReadSrc(String path) throws IOException{
String Src="";
FileReader read = null;
BufferedReader br = null;
String word = null;
try{
read = new FileReader(path);
br = new BufferedReader(read);
word = br.readLine();
while(word!=null){
Src+=word;
word=br.readLine();
}
}
catch(IOException e){
}finally{
if(read!=null){
read.close();
}
if(br!=null){
br.close();
}
}
return Src;
}
public static Map<String , Integer> Count(String wenzhang){
Map<String , Integer> endmap = new HashMap<String , Integer>();
String src = wenzhang;
src=src.replaceAll(",", " ");
src=src.replace(".", " ");
String[] ss = src.split(" ");
for(String s:ss){
if(s.toString().trim().length()!=0){
if(endmap.containsKey(s)){
endmap.put(s, endmap.get(s)+1);
}
else{
endmap.put(s, 1);
}
}
}
return endmap;
}
}