Skip to content

Instantly share code, notes, and snippets.

@josinSbazin
Created July 13, 2016 15:33
Show Gist options
  • Save josinSbazin/40dcc878e2f9bfa2f67cfc92ee71d28b to your computer and use it in GitHub Desktop.
Save josinSbazin/40dcc878e2f9bfa2f67cfc92ee71d28b to your computer and use it in GitHub Desktop.
level18.lesson10.home08
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.*;
/* Нити и байты
Читайте с консоли имена файлов, пока не будет введено слово "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) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName;
try {
while (!(fileName = reader.readLine()).equals("exit")) {
new ReadThread(fileName).start();
}
reader.close();
fileName = null;
} catch (IOException ignore) {/*NOP*/}
}
public static class ReadThread extends Thread {
private String fileName;
public ReadThread(String fileName) {
this.fileName = fileName;
}
@Override
public void run() {
String result;
int count;
int tempCount;
try
{
FileInputStream fis = new FileInputStream(fileName);
int data;
ArrayList<Integer> bytes = new ArrayList<Integer>();
while (fis.available()>0) {
bytes.add(fis.read());
}
int a = 0;
Integer max = 0;
for (Integer b : bytes) {
int c = Collections.frequency(bytes, b);
if (c>a) {
a=c;
max = b;
}
}
resultMap.put(fileName, max);
fis.close();
}catch (IOException ignore) {/*NOP*/}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment