Hi, I think that my logic is close to being correct, but I can't get this to pass verification. I don't know if I've used the renameFile() method in proper way. Next thing that may not be working is getFilePaths- should i check (in the body of loop) if a file is directory, and if it is, call the method recursively? Or maybe there is something wrong in writing contents to the file?
Please help me, I've run out of ideas how to solve this.
(also strange thing happens if i move the "renaming file" part after the "calling method" part- the verification shows that everything is wrong except the last condition)
package com.codegym.task.task31.task3101;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
/*
Iterating through a file tree
*/
public class Solution {
ArrayList<String> filePaths = new ArrayList<>();
// method for filling the list with appropriate files
public void getFilePaths(File file){
File folder = file.getParentFile();
for (File innerFile : folder.listFiles()){
if (!innerFile.isDirectory() && innerFile.length() < 50){
filePaths.add(innerFile.getAbsolutePath());
}
}
}
public static void main(String[] args) throws IOException {
String path = args[0];
String resultFileAbsolutePath = args[1];
File sourceFile = new File(path);
File outputFile = new File(resultFileAbsolutePath);
//renaming the output file and creating output stream
File newOutputFile = new File(outputFile.getParent() + "\\allFilesContent.txt");
FileUtils.renameFile(outputFile, newOutputFile);
FileOutputStream fos = new FileOutputStream(newOutputFile);
//calling the method
Solution solution = new Solution();
solution.getFilePaths(sourceFile);
Collections.sort(solution.filePaths);
// writing contents of the files to output file
for (String fileName : solution.filePaths){
File file = new File(fileName);
BufferedReader br = new BufferedReader(new FileReader(file));
while (br.ready()){
String line = br.readLine();
fos.write(line.getBytes());
}
fos.write("\\n".getBytes());
br.close();
}
fos.close();
}
}