How do I write a method in java that reads a text file and finds the number of occurrences of each character?

advertisements

How do I write a method in java that reads a text file and finds the number of occurrences of each character? Then after it stores the items into an arraylist as you read them.


First open the .txt file and read its contents then create a Hashmap and iterate over the characters for counting

    BufferedReader br = new BufferedReader(new FileReader(fileName));
    HashMap<Character,Integer> hm;
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
         //iterate over the string and increment
         //hashmap.get("character") by one
         line = br.readLine();
    }