Hi,
For some reason rename doesn't work. I was using both FileUtils method and native File class method.
Please, help me to figure the problem.
Thank you!
package com.codegym.task.task31.task3101;
import java.io.*;
import java.nio.file.Files;
import java.util.*;
/*
Iterating through a file tree
*/
public class Solution {
public List<File> foundFiles = new ArrayList<>();
public static void main(String[] args) throws IOException {
File path = new File(args[0]);
File resultFileAbsolutePath = new File(args[1]);
Solution solution = new Solution();
solution.findAndFillFiles(path);
File targetFile = new File(resultFileAbsolutePath.getParentFile().getAbsolutePath() + File.separator + "allFilesContent.txt");
FileUtils.renameFile(resultFileAbsolutePath, targetFile);
try (
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetFile));
) {
Collections.sort(solution.foundFiles, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return o1.getName().compareTo(o2.getName());
}
});
for (File file : solution.foundFiles) {
System.out.println(file.getName());
byte[] fileContent = Files.readAllBytes(file.toPath());
bufferedOutputStream.write(fileContent);
bufferedOutputStream.write("\n".charAt(0));
}
}
}
public void findAndFillFiles(File file) {
for (File childFile : file.listFiles()) {
if (childFile.isDirectory()) {
findAndFillFiles(childFile);
} else if (childFile.length() <= 50) {
foundFiles.add(childFile);
}
}
}
}