-
Notifications
You must be signed in to change notification settings - Fork 0
/
File3.java
46 lines (33 loc) · 1.29 KB
/
File3.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
import java.io.*;
import java.util.*;
public class data {
public static void main(String[] args) {
try{
//Creating the BufferedReader to read the file
File textFile = new File("C:\\Users\\TCS\\eclipse-workspace\\Twest\\src\\one.txt");
BufferedReader input = new BufferedReader(new FileReader(textFile));
//Creating the Map to store the words and their occurrences
TreeMap<String, Integer> frequencyMap = new TreeMap<String, Integer>();
String currentLine = null;
//Reading line by line from the text file
while((currentLine = input.readLine()) != null){
//Parsing the words from each line
StringTokenizer parser = new StringTokenizer(currentLine, " \t\n\r\f.,;:!?'\"");
while(parser.hasMoreTokens()){
String currentWord = parser.nextToken();
Integer frequency = frequencyMap.get(currentWord);
if(frequency == null){
frequency = 0;
}
//Putting each word and its occurrence into Map
frequencyMap.put(currentWord, frequency + 1);
}
}
//Displaying the Result
System.out.println(frequencyMap);
}catch(IOException ie){
ie.printStackTrace();
System.err.println("Your entered path is wrong");
}
}
}