Я вроде всё сделал правильно, все потоки закрыл, пишу в файл в правильном порядке, но он не принимает.
Скажите. пожалуйста, где я неправ?
Поработал над программой: 4 текстовых файла склеивает в правильном порядке на локальном компьютере, новый файл создаёт правильно в папке, где лежат прочитанные файлы, ничего не падает в процессе выполнения.
Потом проделал то же с mp3-файлами - всё работает.
Вот видео об этом, например.
Фильм о склейке mp3-файлов
Не знаю, что ему нужно.
Вот, что он мне пишет:
А вот какой код:
import java.io.*;
import java.util.*;
/*
Собираем файл
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
List<String> fnsList = new ArrayList<>();
List<Thread> threads = new ArrayList<>();
Map<String, byte[]> resultDict = new HashMap<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String buf;
FileOutputStream fos = null;
String outName;
try {
while (!(buf = reader.readLine()).equals("end")) {
Thread t = new ReadThread(buf);
t.start();
threads.add(t);
}
synchronized (threads) {
for (Thread x : threads
) {
resultDict.put(((ReadThread) x).getFn(), ((ReadThread) x).getBuffer());
}
}
fnsList.addAll(resultDict.keySet());
Collections.sort(fnsList, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return (getLastDidits(reverse(o1.split("[.]")).get(0)) - getLastDidits(reverse(o2.split("[.]")).get(0)));
}
});
String[] temp;
String path="";
if (fnsList.get(0).indexOf("/") != -1) {
temp = fnsList.get(0).split("/");
for (int i = 0; i < temp.length - 1; i++) {
path += temp[i] + "/";
}
}
else{
temp=fnsList.get(0).split("[\\\\]");
for (int i = 0; i < temp.length - 1; i++) {
path += temp[i] + "\\\\";
}
}
System.out.println(path);
File myPath = new File(path);
File file = new File(myPath, "myFile");
outName = path + "myFile";
//System.out.println(outName);
fos = new FileOutputStream(outName, true);
for (String x : fnsList
) {
fos.write(resultDict.get(x));
}
} catch (IOException e) {
e.printStackTrace();
e.getMessage();
} finally {
try {
reader.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static List<String> reverse(String[] arr) {
List<String> list = Arrays.asList(arr);
Collections.reverse(list);
return list;
}
public static int getLastDidits(String s) {
return Integer.parseInt(s.replaceAll("part", ""));
}
public static class ReadThread extends Thread {
private byte[] buffer;
private String fn;
FileInputStream fis;
public String getFn() {
return fn;
}
public ReadThread(String fn) throws FileNotFoundException {
this.fn = fn;
fis = new FileInputStream(this.fn);
}
public void run() {
byte[] b;
int size = 0;
try {
size = fis.available();
b = new byte[size];
fis.read(b);
buffer = b;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public byte[] getBuffer() {
return buffer;
}
}
}