Advertisement
jaVer404

level18.lesson10.home08(works but not tested)

Jan 14th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. package com.javarush.test.level18.lesson10.home08;
  2.  
  3. import java.io.*;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.Map;
  7.  
  8. /* Нити и байты
  9. Читайте с консоли имена файлов, пока не будет введено слово "exit"
  10. Передайте имя файла в нить ReadThread
  11.  
  12. Нить ReadThread должна найти байт, который встречается в файле максимальное число раз,
  13. и добавить его в словарь resultMap,
  14. где параметр String - это имя файла, параметр Integer - это искомый байт.
  15.  
  16. Закрыть потоки. Не использовать try-with-resources
  17. */
  18. /*
  19. 1. В main, пока не ввели слово exit считываются имена файлов
  20. 2. Имя файла передается в нить ReadThread (как???)
  21. 3. Нить читает и считает файл и файл и байт в словарь.
  22. Проблема:
  23.  1. Конструктор ReadThread
  24. */
  25.  
  26. /*
  27. *Пока не ввели exit создаем (через конструктор) новую нить ReadThread
  28.  Передаем туда имя файла с консоли, она читает файл, считает и добавляет в словарь.
  29. *
  30. *
  31. * */
  32.  
  33. public class Solution {
  34.     public static Map<String, Integer> resultMap = new HashMap<String, Integer>();
  35.  
  36.     public static void main(String[] args) throws IOException {
  37.        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  38.         String consoleInput = "";
  39.         while (true) {
  40.             consoleInput=reader.readLine();
  41.             if (consoleInput.equals("exit")) {
  42.                 break;
  43.             }
  44.             else {
  45.  
  46.                 new ReadThread(consoleInput).start();//it would be different threads
  47.             }
  48.         }
  49.     }
  50.  
  51.     public static class ReadThread extends Thread {
  52.         public ReadThread(String fileName) {
  53.             //implement constructor body
  54.             super(fileName);
  55.         }
  56.  
  57.  
  58.         // implement file reading here - реализуйте чтение из файла тут
  59.         public void run() {
  60.             try {
  61.                 String fileName = this.getName();
  62.                 byte[]bytes = fileToArray(this.getName());
  63.                 HashSet<Byte>setBytes = countBytes(bytes);
  64.                 resultMap.put(fileName,(int)returnByte(bytes, setBytes));
  65.             }
  66.             catch (Exception e) {}
  67.         }
  68.     }
  69.  
  70.  
  71.     public static byte[] fileToArray (String fileName) throws IOException
  72.     {
  73.  
  74.             File file = new File(fileName);
  75.             byte[] bFile = new byte[(int) file.length()];
  76.             FileInputStream  fileInputStream = new FileInputStream(file);
  77.             fileInputStream.read(bFile);
  78.             fileInputStream.close();
  79.             return bFile;
  80.     }
  81.  
  82.     public static HashSet<Byte>countBytes (byte[]bytesArray) {
  83.         HashSet<Byte> allBytes = new HashSet<Byte>();
  84.         for (int i = 0; i < bytesArray.length;i++) {
  85.             allBytes.add(bytesArray[i]);
  86.         }
  87.         return allBytes;
  88.     }
  89.  
  90.     public static int countSpecByte (byte myByte, byte[]someArray)/*Не нужно тут выкидывать исключения*/
  91.     {
  92.         int counter = 0;
  93.         for (byte b : someArray) {
  94.             if (myByte == b) {
  95.                 counter++;
  96.             }
  97.         }
  98.         return  counter;
  99.     }
  100.  
  101.     public static int returnByte (byte[]someBytes, HashSet<Byte>someUniqeBytes) {
  102.         HashMap<Byte, Integer> bytesAndCounts = new HashMap<Byte, Integer>();
  103.         for (byte specByte : someUniqeBytes) {
  104.             bytesAndCounts.put(specByte, countSpecByte(specByte,someBytes));
  105.         }
  106.         Map.Entry<Byte, Integer> maxEntry = null;
  107.  
  108.         for (Map.Entry<Byte, Integer> entry : bytesAndCounts.entrySet())
  109.         {
  110.             if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
  111.             {
  112.                 maxEntry = entry;
  113.             }
  114.         }
  115.         return maxEntry.getValue();
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement