Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level18.lesson03.task03;
- import java.io.BufferedReader;
- import java.io.ByteArrayOutputStream;
- import java.io.FileInputStream;
- import java.io.InputStreamReader;
- import java.util.*;
- /* Самые частые байты
- Ввести с консоли имя файла
- Найти байт или байты с максимальным количеством повторов
- Вывести их на экран через пробел
- Закрыть поток ввода-вывода
- */
- public class Solution {
- public static void main(String[] args) throws Exception {
- String fileName = new BufferedReader(new InputStreamReader(System.in)).readLine();
- FileInputStream fileInputStream = new FileInputStream(fileName);
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- while (fileInputStream.available() > 0) {
- stream.write(fileInputStream.read());
- }
- byte[]bytes=stream.toByteArray();
- /*------------------------------------------------*/
- Map<Byte, Integer> bytesMap = new HashMap<Byte,Integer>();
- for (Byte b : bytes) {
- Integer freq = bytesMap.get(b);
- bytesMap.put(b,(freq==null)?1 : freq+1);
- }
- /*------------------------------------------------*/
- int maxValue = (Collections.max(bytesMap.values()));
- ArrayList<Byte> bigBytes = new ArrayList<Byte>();
- for (Map.Entry<Byte,Integer>entry : bytesMap.entrySet()) {
- if (entry.getValue()==maxValue) {
- bigBytes.add(entry.getKey());
- }
- }
- for (Byte b : bigBytes) {
- System.out.print(b);
- System.out.print(" ");
- }
- fileInputStream.close();
- System.in.close();
- System.out.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement