package com.codegym.task.task31.task3101;
/*
Iterating through a file tree
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
public class Solution {
public static void main(String[] args) {
String path = args[0];
String resultFileAbsolutePath = args[1];
TreeMap<String, byte[]> tree = new TreeMap<>(String::compareTo);
File file = new File(path);
generateTree(file, tree);
try {
File resultFile = new File(resultFileAbsolutePath);
File newResultFile = new File(resultFile.getParentFile() + "/allFilesContent.txt");
if (FileUtils.isExist(newResultFile)) { FileUtils.deleteFile(newResultFile); }
FileUtils.renameFile(resultFile, newResultFile);
FileUtils.renameFile(resultFile, newResultFile);
try (FileOutputStream fileOutputStream = new FileOutputStream(newResultFile)) {
for (byte[] bytes : tree.values()) {
fileOutputStream.write(bytes);
fileOutputStream.write("\n".getBytes());
}
}
} catch (IOException ignored) {}
}
public static void generateTree(File root, TreeMap tree) {
Arrays.stream(Objects.requireNonNull(root.listFiles())).forEach(file -> {
if (file.isDirectory()) { generateTree(file, tree); }
else if (file.length() < 51) {
try { tree.put(file.getName(), Files.readAllBytes(Paths.get(file.getPath()))); }
catch (IOException ignored) {}
}
});
}
}
Dyrits
Level 22
Impossible to get the first requirement right~
Archived
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
2 November 2020, 14:20solution
Renaming a file will not work if the source file (the one to be renamed) doesn't exist; say, for example, if it were deleted before a rename occurred.
+2
Dyrits
2 November 2020, 16:02
Yeah, it makes sense. I took that line from the solution in fact, after failing too many times...
Which is weird, no?
It should be deleted afterwards.
0
Guadalupe Gagnon
2 November 2020, 16:18
The original file name won't exist once renamed, so deleting it after doesn't make sense either.
+1
Dyrits
2 November 2020, 16:23
I'm still confused about the solution provided which delete the file doing so:
0
Guadalupe Gagnon
2 November 2020, 16:40
I see, I read your code wrong at first and thought you were deleting the source file before the rename, but you were deleting the new file name if it already existed. Lucky it turned out to be the "correct" answer. I don't see why that would fail as opposed to not deleting anything, as the result would still be the same.
I don't know. The task requirements seem a little jumbled up (we are all shocked) and reading them I can see that it would suggest that if the destination file already exists then you would need to delete it so that you can read the source file. All I can say is that a lot of these tasks I personally passed by changing small things every time the validation failed until I finally passed. Sometimes quite a few times.
+1
Dyrits
2 November 2020, 22:08
Which is confusing is that the deletion happens before renaming the file. We create a file, we delete it and rename the other one according to the deleted one?
+1
Guadalupe Gagnon
3 November 2020, 14:16
Creating a File object in code (e.g. new File(fileName)) does not create a file on the hard drive. You would have to specifically call the .createNewFile() method of the File class to do that. Opening output streams will automatically create a file as well if the file to output to does not yet exist.
+1