Hello EveryOne,
As you read in the title, the code works correctly and seems covering the requirements ! but the validator keeps refusing to validate it, thanks in advance for your help.
The file passed as the second argument to main must be renamed to allFilesContent.txt. => Line 21
You need to create a stream to write to the renamed file. => Line 26
The output stream to the file must be closed => Line 42
package com.codegym.task.task31.task3101;
/*
Iterating through a file tree
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Solution {
public static void main(String[] args) throws IOException {
String path = args[0];
File storingFile = new File(args[1]);
ArrayList<File> tabFile = new ArrayList<File>();
String data;
RecursiveDirectory (new File(path), tabFile);
Collections.sort(tabFile, new fileComparator());
File fileResult = new File(storingFile.getParentFile()+ "/allFilesContent.txt");
if (FileUtils.isExist(fileResult)) {
FileUtils.deleteFile(fileResult);
}
FileUtils.renameFile(storingFile,fileResult);
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileResult.getAbsolutePath()));
for (File file: tabFile) {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file.getAbsolutePath()));
try {
while ((data = bufferedReader.readLine()) != null) {
bufferedWriter.append(new String(data));
}
}
finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
bufferedWriter.newLine();
}
bufferedWriter.close();
}
static void RecursiveDirectory (File directory,ArrayList<File> tabFile) {;
for (File file: directory.listFiles()) {
if (file.isFile()) {
if (file.length() <= 50) {
tabFile.add(file);
}
}
else {
RecursiveDirectory (file, tabFile);
}
}
}
}
class fileComparator implements Comparator<File> {
// override the compare() method
public int compare(File f1, File f2)
{
return f1.getName().compareTo(f2.getName());
}
}