Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 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.Map;
  6.  
  7. /* Нити и байты
  8. Читайте с консоли имена файлов, пока не будет введено слово "exit"
  9. Передайте имя файла в нить ReadThread
  10. Нить ReadThread должна найти байт, который встречается в файле максимальное число раз, и добавить его в словарь resultMap,
  11. где параметр String - это имя файла, параметр Integer - это искомый байт.
  12. Закрыть потоки. Не использовать try-with-resources
  13. */
  14.  
  15. public class Solution
  16. {
  17.     public static Map<String, Integer> resultMap = new HashMap<String, Integer>();
  18.  
  19.     public static void main(String[] args) throws IOException, InterruptedException
  20.     {
  21.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  22.         while (true)
  23.         {
  24.             String str = br.readLine();
  25.             if (str.equals("exit"))
  26.                 break;
  27.             ReadThread rt = new ReadThread(str);
  28.             rt.start();
  29.             rt.join();
  30.         }
  31.         br.close();
  32.         for (Map.Entry<String, Integer> stringIntegerEntry : resultMap.entrySet())
  33.         {
  34.             System.out.println(stringIntegerEntry.getKey() + " " + stringIntegerEntry.getValue());
  35.         }
  36.     }
  37.  
  38.     public static class ReadThread extends Thread
  39.     {
  40.         File file;
  41.         private FileInputStream fis;
  42.         byte[] buf;
  43.         String fileName;
  44.  
  45.         public ReadThread(String fileName) throws IOException
  46.         {
  47.             this.fileName = fileName;
  48.             file = new File(fileName);
  49.             fis = new FileInputStream(file);
  50.             buf = new byte[(int) file.length()];
  51.         }
  52.  
  53.         public void run()
  54.         {
  55.             try
  56.             {
  57.                 fis.read(buf);
  58.                 search();
  59.                 fis.close();
  60.             }
  61.             catch (IOException e)
  62.             {
  63.                 e.printStackTrace();
  64.             }
  65.         }
  66.  
  67.         public void search()
  68.         {
  69.  
  70.  
  71.             int[] array = new int[256];
  72.             for (int i = 0; i < buf.length; i++)
  73.             {
  74.                 array[buf[i] + 128]++;
  75.             }
  76.  
  77.             int max = 0;
  78.             int index = 0;
  79.             for (int i = 0; i < array.length; i++)
  80.                 if (array[i] > max)
  81.                 {
  82.                     max = array[i];
  83.                     index = i;
  84.                 }
  85.             index -= 128;
  86.             System.out.println(index);
  87.             resultMap.put(fileName, index);
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement