101. Read Count of Unique Word from a file - prabhatrocks07/Core-Java-Programming GitHub Wiki

public class ReadUniqueWordFromFile {

public static void main(String[] args) {
	String str = "Prabhat is a good boy. Good boy always follows the rule. It is nice to be good";
	try {
		Writer out = new BufferedWriter(new FileWriter("myfile.txt"));
		out.write(str);
		out.flush();
		out.close();
		
		Scanner sc = new Scanner(new File("myfile.txt"));
		List<String> list = new ArrayList<String>();
		while (sc.hasNext()) {
			list.add(sc.next());
		}
		sc.close();
		
		System.out.println(list.size());
		Set<String> set = new HashSet<>(list);
		
		for (String s : set) {
			System.out.println(s + ": " + Collections.frequency(list, s));
		}
	} catch (IOException e) {
		e.printStackTrace();
	}

}

}

⚠️ **GitHub.com Fallback** ⚠️