вроде логика выполняется, файл переименовывается, при этом используется метод rename, однако пишут, что необходимо его использовать, поток вроде должен закрываться с trywith, но видимо валидатор это не принимает, так как не видит его создание. Подскажите, пожалуйста, что я упустил
package com.javarush.task.task31.task3101;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
/*
Проход по дереву файлов
*/
public class Solution {
private static class FileVisitor extends SimpleFileVisitor<Path>{
private final List <Path> filesPath = new ArrayList<>();
public List<Path> getSortedPaths(){
List<String> stringPaths = new ArrayList<>();
for (Path path:filesPath){
stringPaths.add(path.toString());
}
Collections.sort(stringPaths);
List <Path> result = new ArrayList<>();
for (String stringPath:stringPaths){
result.add(Paths.get(stringPath));
}
return result;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (!Files.isDirectory(file)){
if (Files.size(file)<=50){
filesPath.add(file);
}
}
return FileVisitResult.CONTINUE;
}
}
public static void main(String[] args) {
Path directoryPath = Paths.get(args[0]);
String resultFileAbsolutePath = args[1];
File resultFile = new File(resultFileAbsolutePath);
File newResultFileName = new File(resultFile.getParentFile()+"/"+"allFilesContent.txt");
if (FileUtils.isExist(resultFile)&&!FileUtils.isExist(newResultFileName)){
FileUtils.renameFile(resultFile,newResultFileName);
}
FileVisitor fileVisitor = new FileVisitor();
try {
Files.walkFileTree(directoryPath,fileVisitor);
} catch (IOException e) {
e.printStackTrace();
}
List<Path> sortedPaths = fileVisitor.getSortedPaths();
StringBuilder result = new StringBuilder();
for (Path paths:sortedPaths){
File currentFile = new File(paths.toString());
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(currentFile))){
while (bufferedReader.ready()){
result.append(bufferedReader.readLine());
}
bufferedReader.close();
result.append("\n");
} catch (IOException e) {
e.printStackTrace();
}
}
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(newResultFileName,true))) {
bufferedWriter.write(result.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}