I tested locally, everyting works. But I can not pass the tests.
package com.codegym.task.task31.task3101;
import java.io.*;
import java.util.*;
/*
Iterating through a file tree
*/
public class Solution {
public static void main(String[] args) throws IOException {
// Get path and file name arguments, rename target file
String path = args[0];
String resultFileAbsolutePath = args[1];
File resultFile = new File(resultFileAbsolutePath);
File newFile = null;
if (FileUtils.isExist(resultFile)) {
String newFileStr = resultFile.getParent() + "/allFilesContent.txt";
newFile = new File(newFileStr);
FileUtils.renameFile(resultFile, newFile);
} else {
System.out.println("File not found. Bye ...");
return;
}
// Find all sub-files & paths
List<String> listFiles = new ArrayList<>();
Map<String, String> mapFiles = new HashMap<>();
Queue<File> directoryQueue = new LinkedList<>();
directoryQueue.add(new File(path));
File[] files;
while (!directoryQueue.isEmpty()) {
files = directoryQueue.poll().listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
directoryQueue.add(file);
} else {
if (file.length() <= 50) {
listFiles.add(file.getName());
mapFiles.put(file.getAbsolutePath(), file.getName());
}
}
}
}
}
// Sort list
Collections.sort(listFiles);
// Write Map to File with relation to sorted list
FileOutputStream fos = new FileOutputStream(newFile.toString());
for (String s : listFiles) {
Iterator<Map.Entry<String, String>> itr = mapFiles.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry<String, String> entry = itr.next();
if (entry.getValue().equals(s)) {
//System.out.println(entry.getValue() + " - " + entry.getKey());
BufferedReader br = new BufferedReader(new FileReader(entry.getKey()));
while (br.ready()) {
fos.write(br.read());
}
br.close();
fos.write("\n".getBytes());
itr.remove();
break;
}
}
}
fos.close();
}
}