Where is the error?
package com.codegym.task.task31.task3101;
/*
Iterating through a file tree
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class Solution {
List<File> foundFilesList = new ArrayList<>();
public static void main(String[] args) throws IOException {
Solution solution = new Solution();
File resultFileAbsolutePath = new File(args[1]);
File destination = new File(resultFileAbsolutePath.getParent() + "/allFilesContent.txt");
if (FileUtils.isExist(destination)) FileUtils.deleteFile(destination);
FileUtils.renameFile(resultFileAbsolutePath, destination);
FileOutputStream fos = new FileOutputStream(destination);
File dir = new File(args[0]);
solution.recursiveDirectory(dir);
Comparator<File> sortByFileName = new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return o1.getName().compareTo(o2.getName());
}
};
solution.foundFilesList.sort(sortByFileName);
for (File file : solution.foundFilesList) {
FileInputStream fis = new FileInputStream(file);
while (fis.available() > 0) {
fos.write(fis.read());
}
if (!solution.foundFilesList.isEmpty())
fos.write("/n".getBytes());
fis.close();
}
fos.close();
}
private void recursiveDirectory(File dir) {
for (File file : Objects.requireNonNull(dir.listFiles())) {
if (file.isFile()) {
if (file.length() <= 50) {
foundFilesList.add(file);
}
} else
recursiveDirectory(file);
}
}
}
/*
Iterating through a file tree
1. The main method is passed two arguments as inputs.
The first argument is path, which is the path to the directory; the second is resultFileAbsolutePath,
which is the name (full path) of an existing file that will store the result.
2. For each file in the path directory and in all of its subdirectories, do the following:
For each file whose size in bytes is NOT greater than 50, do the following:
2.1. Sort them by file name in ascending order. Don't include the path when sorting.
2.2. Rename resultFileAbsolutePath to "allFilesContent.txt" (use the FileUtils.renameFile
method and FileUtils.isExist if necessary).
2.3. Sequentially write the contents of each file from step 2.1 to allFilesContent.txt. After the body
of each file, write "\n".
All files have the TXT extension.
Use "/" as the path separator.
Requirements:
1. The file passed as the second argument to main must be renamed to allFilesContent.txt.
2. You need to create a stream to write to the renamed file.
3. The contents of every file that is 50 bytes or smaller must be written to the allFilesContent.txt file.
mentor: Be sure that nothing superfluous is written to the allFilesContent.txt file.
4. The output stream to the file must be closed.
5. Don't use static variables.
*/