I implemented recursive file collection from all inner folders, but my code do not pass. What is wrong?
package com.codegym.task.task31.task3101;
/*
Iterating through a file tree
*/
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
String fullName = args[0] + "/" + args[1]; // args[0] - path, args[1] - name
File container = new File(fullName);
Path pathToContainer = Paths.get(container.getParent());
File newContainer = new File(pathToContainer + "/" + "allFilesContent.txt");
FileUtils.renameFile(container, newContainer);
File path = new File(newContainer.getParent());
File[] files = path.listFiles();
List<File> allFiles = new ArrayList<>();
getAllFiles(allFiles, files);
FileOutputStream output = new FileOutputStream(newContainer);
sortFiles(allFiles);
//System.out.println(allFiles);
for (File file : allFiles) {
if (file.equals(newContainer)) {
continue;
}
if (file.length() <= 50) {
FileInputStream input = new FileInputStream(file);
int i=-1;
while((i = input.read()) != -1) {
output.write(i);
//System.out.println((char) i);
}
output.write('\n');
output.flush();
input.close();
}
}
output.close();
}
private static void sortFiles(List<File> allFiles) {
for (int i = 0; i < allFiles.size(); i++) {
for (int j = i + 1; j < allFiles.size(); j++) {
if (allFiles.get(i).getName().compareTo(allFiles.get(j).getName()) > 0) {
File temp = allFiles.get(i);
allFiles.set(i, allFiles.get(j));
allFiles.set(j, temp);
}
}
}
}
private static void getAllFiles(List<File> allFiles, File[] files) {
for (File file : files) {
if (file.isDirectory()) {
File[] names = file.listFiles();
getAllFiles(allFiles, names);
} else {
allFiles.add(file);
}
}
}
}