The task is to rename some existing file and after that write some data into it.
There is a special handmade class for renaming, which uses File.renameTo method.
But that's what program does:
New file with new name appears in directory and nothing writes into it, but old file with previose name stays in that directory also (it shouldn't I suppose). And all data writes into that old file.
Here is that handmade class:
public class FileUtils {
public static void renameFile(File source, File destination) {
if (!source.renameTo(destination)) {
System.out.println("Can not rename file with name " + source.getName());
}
}
}
And here is my code:
public class Solution {
public static void main(String[] args) throws IOException {
//args[0] = "D:/Java_Projects/FileJob";
//args[1] = "D:/Java_Projects/result.txt";
Path path = Paths.get(args[0]);
File resultFileAbsolutePath = new File(args[1]);
//File allFilesContent = new File("D:/Java_Projects/allFilesContent.txt");
FileUtils.renameFile(resultFileAbsolutePath, new File("D:/Java_Projects/allFilesContent.txt"));
List<String> filesToWrite = getContent(path);
try (FileOutputStream writeToFile = new FileOutputStream(resultFileAbsolutePath)) {
for (String file : filesToWrite) {
FileInputStream readFile = new FileInputStream(file);
while (readFile.available() > 0) {
writeToFile.write(readFile.read());
}
writeToFile.write('\n');
}
}
}
public static List<String> getContent(Path file) throws IOException {
Path path = Paths.get(file.toString());
Iterator iterator = new Iterator();
Files.walkFileTree(path, iterator);
return iterator.suitableFiles;
}
public static class Iterator extends SimpleFileVisitor<Path> {
List<String> suitableFiles = new ArrayList<>();
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toFile().length() <= 50) {
suitableFiles.add(file.toFile().getAbsolutePath());
}
return CONTINUE;
}
}
}
I tried to use File.renameTo directly in main. Nothing.
I tried to initialese new variable and asign old ref to it. But it seemse to me that File object is immutable. So nothing.
Any ideas?