public class Solution { public static void main(String[] args) throws IOException { FileInputStream reader = new FileInputStream(args[0]); ArrayList<Byte> list = new ArrayList<>(); Map<Byte, Integer> map = new HashMap<>(); int a; while ((a = reader.read()) > -1){ if (a <= 127) { list.add((byte)a); } } for (int i = 0; i < list.size()-1; i++) { if (map.containsKey(list.get(i))) { map.put(list.get(i), map.get(list.get(i))+1); } else { map.put(list.get(i),1); } } Map<Byte, Integer> treeMap = new TreeMap<>(map); for (byte k: treeMap.keySet()){ System.out.println((char)k + " " + treeMap.get(k)); } reader.close(); } }