Hi all, could you please give me some tips?
package com.codegym.task.task18.task1825;
import java.io.*;
import java.util.*;
/*
Building a file
*/
public class Solution {
public static void main(String[] args) {
HashMap<String,ArrayList<String>> hashmap = new HashMap<>();
BufferedReader bufferedReader = null;
try{
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
String extension = null;
while (true){
line = bufferedReader.readLine();
if (line.matches("^end")) break;
if (line.contains(".")) {
extension = line.split("\\.")[0] + "." + line.split("\\.")[1];
}
if (hashmap.containsKey(extension)){
hashmap.get(extension).add(line);
}else
if (!hashmap.containsKey(extension)){
hashmap.put(extension, new ArrayList<>() );
hashmap.get(extension).add(line);
}
}
}catch(Exception e){
e.printStackTrace();
}finally {
try{
if (bufferedReader != null) bufferedReader.close();}
catch (IOException io){
io.printStackTrace();
}
}
//put in the correct order
for(Map.Entry<String,ArrayList<String>> input : hashmap.entrySet()) {
ArrayList<String> tmpArraylist = input.getValue();
String tmpInput;
for (int i = 0; i<tmpArraylist.size();i++){
for (int k = i+1; k<tmpArraylist.size();k++){
Integer iInteger = Integer.valueOf( tmpArraylist.get(i).split("part")[1] );
Integer kInteger = Integer.valueOf( tmpArraylist.get(k).split("part")[1] );
//System.out.println(iInteger);
//System.out.println(kInteger);
if (iInteger>kInteger){
tmpInput = tmpArraylist.get(i);
tmpArraylist.set(i, tmpArraylist.get(k));
tmpArraylist.set(k, tmpInput);
}
}
}
//System.out.println(tmpArraylist.toString());
hashmap.put(input.getKey(),tmpArraylist);
}
/*
for(Map.Entry<String,ArrayList<String>> input : hashmap.entrySet()) {
for (String s : input.getValue()){
System.out.println(s);
}
}
*/
BufferedReader fileInputStream = null;
BufferedWriter fileWriter = null;
try{
File file = null;
File file1 = null;
for(Map.Entry<String,ArrayList<String>> input : hashmap.entrySet()) {
file = new File(input.getKey());
fileWriter = new BufferedWriter( new FileWriter(file) );
for (String s : input.getValue()) {
file1 = new File(s);
fileInputStream = new BufferedReader( new FileReader( file1) );
String inputString;
while ( ( inputString = fileInputStream.readLine() )!= null){
fileWriter.write(inputString+"\n");
//fileWriter.flush();
//fileWriter.newLine();
}
fileInputStream.close();
}
fileWriter.close();
}
}catch(Exception e){
e.printStackTrace();
}finally {
try{
if (fileWriter != null) fileWriter.close();}
catch (IOException io){
io.printStackTrace();
}
try{
if (fileInputStream != null) fileInputStream.close();}
catch (IOException io){
io.printStackTrace();
}
}
}
}