Java FileInputStream count character frequency

Description

Java FileInputStream count character frequency

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Main {
   public static void main(String[] args) throws Exception {
      Map<Character, Integer> m = new HashMap<>();
      FileInputStream fis = new FileInputStream(new File("Main.java"));
      while (fis.available() > 0) {
         char a = (char) fis.read();
         Integer freq = (Integer) m.get(a);
         m.put(a, (freq == null) ? 1 : freq + 1);
      }//w ww .ja v a2  s . co  m
      fis.close();
      Set<Character> keys = m.keySet();
      for (Character k : keys) {
         System.out.println(k + "->" + m.get(k));
      }
   }
}



PreviousNext

Related