Works, but getting errors. I believe I have set the comparator so that the TreeMap works as it should. Can't see anything else??
package com.codegym.task.task18.task1825;
import java.io.*;
import java.util.*;
/*
Building a file
*/
public class Solution {
public static void main(String[] args) throws Exception {
TreeMap<String, String> fileMap = new TreeMap<>(new NewComparator());
String outFile = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String fileName = reader.readLine();
if (fileName.equals("end")) break;
else {
String prefix, suffix;
int lastDotIndx = fileName.lastIndexOf('.');
if (lastDotIndx < 0) outFile = fileName;
else if (!(fileName.substring(lastDotIndx).matches(".part\\d+"))) outFile = fileName;
else {
fileMap.put(fileName.substring(lastDotIndx + 5), fileName.substring(0, lastDotIndx + 5));
}
}
}
FileOutputStream fos = new FileOutputStream(outFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
for (Map.Entry<String, String> entry : fileMap.entrySet()) {
String name = entry.getValue() + entry.getKey();
FileInputStream fis = new FileInputStream(name);
BufferedInputStream bis = new BufferedInputStream(fis);
int i;
while ((i = bis.read()) != -1) {
bos.write(i);
}
bis.close();
fis.close();
}
bos.close();
fos.close();
}
}
class NewComparator implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
int i1 = Integer.parseInt(s1);
int i2 = Integer.parseInt(s2);
return Integer.compare(i1, i2);
}
}