2. Для чтения из файла используй поток FileInputStream.
5 .Поток чтения из файла должен быть закрыт.
public class Solution {
public static void main(String[] args) throws Exception {
String fileName = "";
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))){
fileName = reader.readLine();
} catch (IOException e){
e.printStackTrace();
}
try (FileInputStream streamValue = new FileInputStream(fileName);
FileInputStream streamCount = new FileInputStream(fileName)) {
Map<Integer, Integer> map = new HashMap<>();
while (streamValue.available() > 0){
map.put(streamValue.read(), 0);
}
while (streamCount.available() > 0){
Integer x = streamCount.read();
if (map.containsKey(x)){
map.put(x, map.get(x) + 1);
}
}
int y = 0;
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
if (y < entry.getValue()) {
y = entry.getValue();
}
}
List<Integer> list = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
if (y == entry.getValue()) {
list.add(entry.getKey());
}
}
for (Integer x: list) {
System.out.print(x + " ");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.javarush.task.task18.task1803;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
Самые частые байты
*/
public class Solution {
public static void main(String[] args) throws Exception {
String fileName = "";
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))){
fileName = reader.readLine();
} catch (IOException e){
e.printStackTrace();
}
try (FileInputStream streamValue = new FileInputStream(fileName);
FileInputStream streamCount = new FileInputStream(fileName)) {
Map<Integer, Integer> map = new HashMap<>();
while (streamValue.available() > 0){
map.put(streamValue.read(), 0);
}
while (streamCount.available() > 0){
Integer x = streamCount.read();
if (map.containsKey(x)){
map.put(x, map.get(x) + 1);
}
}
int y = 0;
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
if (y < entry.getValue()) {
y = entry.getValue();
}
}
List<Integer> list = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
if (y == entry.getValue()) {
list.add(entry.getKey());
}
}
for (Integer x: list) {
System.out.print(x + " ");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}