Advertisement
Guest User

Untitled

a guest
May 5th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. /* Нити и байты
  2. Читайте с консоли имена файлов, пока не будет введено слово "exit"
  3. Передайте имя файла в нить ReadThread
  4. Нить ReadThread должна найти байт, который встречается в файле максимальное число раз, и добавить его в словарь resultMap,
  5. где параметр String - это имя файла, параметр Integer - это искомый байт.
  6. Закрыть потоки. Не использовать try-with-resources
  7. */
  8.  
  9. public class Solution {
  10.     public static Map<String, Integer> resultMap = new HashMap<String, Integer>();
  11.  
  12.     public static void main(String[] args) throws IOException {
  13.  
  14.  
  15.         BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
  16.         String tmp = null;
  17.  
  18.         while (!(tmp = r.readLine()).equals("exit")){
  19.             new ReadThread(tmp);
  20.         }
  21.  
  22.         for (Map.Entry<String,Integer> set : resultMap.entrySet()){
  23.             System.out.println(set.getKey() +" "+ set.getValue());
  24.         }
  25.         r.close();
  26.  
  27.     }
  28.  
  29.     public static class ReadThread extends Thread {
  30.  
  31.         public ReadThread(String fileName) {
  32.             super(fileName);
  33.             this.start();
  34.         }
  35.  
  36.         @Override
  37.         public void run()
  38.         {
  39.             List<Integer> bytes = new ArrayList<>();
  40.             int count = 0;
  41.             int maxByte = 0;
  42.  
  43.             try{
  44.                 FileInputStream read = new FileInputStream(super.getName());
  45.                 while (read.available()>0){
  46.                     bytes.add(read.read());
  47.                 }
  48.                 read.close();
  49.  
  50.                 for (Integer a: bytes){
  51.                     int tmpcount = 0;
  52.  
  53.                     for (Integer b: bytes){
  54.                         if (a==b) tmpcount++;
  55.                     }
  56.                     if (count<tmpcount) {
  57.                         count=tmpcount;
  58.                     maxByte = a;
  59.                     }
  60.                     resultMap.put(super.getName(),maxByte);
  61.  
  62.                 }
  63.  
  64.             }
  65.             catch (IOException e){}
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement