Enter a file name from the console. Find the byte or bytes with the maximum number of repetitions. Display them on the screen, separated by spaces. Close the IO stream.
How to solve this task?
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
FileInputStream inputStream = new FileInputStream(fileName);
Map<Integer, Integer> map = new HashMap<>();
int data;
Integer max = 0;
while (inputStream.available() > 0) {
data = inputStream.read();
if (map.containsValue(data)) map.replace(data, map.get(data) + 1);
else map.put(data, 1);
if (map.get(data) > max) max = map.get(data);
}
inputStream.close();
for (Map.Entry<Integer, Integer> entry : map.entrySet()){
if(entry.getValue() > max) {
System.out.print(entry.getKey() + "/t");
}
}
}
}