Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level18.lesson10.home08;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- /* Нити и байты
- Читайте с консоли имена файлов, пока не будет введено слово "exit"
- Передайте имя файла в нить ReadThread
- Нить ReadThread должна найти байт, который встречается в файле максимальное число раз, и добавить его в словарь resultMap,
- где параметр String - это имя файла, параметр Integer - это искомый байт.
- Закрыть потоки. Не использовать try-with-resources
- */
- public class Solution {
- public static Map<String, Integer> resultMap = new HashMap<String, Integer>();
- public static void main(String[] args) throws IOException{
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String consoleInput = "";
- while (true) {
- consoleInput=reader.readLine();
- if (consoleInput.equals("exit")) {
- break;
- }
- else {
- new ReadThread(consoleInput).start();//it would be different threads
- }
- }
- reader.close();
- /* for (Map.Entry<String, Integer> entry : resultMap.entrySet()) {
- String key = entry.getKey().toString();;
- Integer value = entry.getValue();
- System.out.println("key: " + key + " value: " + value );
- }*/
- }
- public static class ReadThread extends Thread {
- public ReadThread(String name)
- { //implement constructor body
- super(name);
- }
- // implement file reading here - реализуйте чтение из файла тут
- @Override
- public void run()
- {
- /*-----------1. Reading bytes to ArrayList-----------------------*/
- ArrayList <Integer> intBytes = new ArrayList<Integer>();
- FileInputStream myFile = null;
- try
- {
- myFile = new FileInputStream(this.getName());
- boolean eof = false;
- while (!eof) {
- int byteValue = myFile.read();
- intBytes.add(byteValue);
- if (byteValue == -1) {
- eof = true;
- }
- }
- }
- catch (IOException e) {
- //System.out.println("On read exception");
- }
- finally
- {
- if (myFile != null) {
- try
- {
- myFile.close();
- }
- catch (Exception e1) {
- //System.out.println("On close exception");
- }
- }
- }
- /*------------------------Ent of file reading--------------------------*/
- //and now we have array list with all Integer (bytes) inside
- //From list we will add them to HashSet (no doubles)
- HashSet<Integer> intByteSet = new HashSet<Integer>();
- for (int i : intBytes) {
- intByteSet.add(i);
- }
- //Now we have Set of uniqe values
- /*----------------------------------------------------------------------*/
- //We take value from intByteSet and count it in the ArrayList
- HashMap<Integer,Integer> mapOfBytes = new HashMap<Integer, Integer>();
- for (int i : intByteSet) {
- mapOfBytes.put(i,countSpecByte(i,intBytes));
- }
- /*----------------------------------------------------------------------*/
- //And now we have HashMap with int key and a number of encounts
- //and we need to find most common byte and add it to the main Map
- /*---------------------------------------------------------------------------*/
- Map.Entry<Integer, Integer> maxEntry = null;
- for (Map.Entry<Integer, Integer> entry : mapOfBytes.entrySet())
- {
- if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)//может тут и два одинаковых влепить
- {
- maxEntry = entry;
- }
- }
- resultMap.put(this.getName(), maxEntry.getKey());
- /*--------------------------------------------------------------------------------------*/
- }
- }
- public static int countSpecByte (int myByte, ArrayList<Integer> arrayInts)/*Не нужно тут выкидывать исключения*/
- {
- int counter = 0;
- for (int b : arrayInts) {
- if (myByte == b) {
- counter++;
- }
- }
- return counter;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement