or do I just not get the question. My code seems to do what the task requires. But it does not want to validate. It even complains about not closed streams. But I use try-with-resources. Or is it really necessary that file 1 gets read into a buffer, file 2 copied to 1 and then the buffer appended?
package de.codegym.task.task18.task1819;
/*
Dateien kombinieren
*/
import java.io.*;
public class Solution {
public static void main(String[] args) {
// Oki, lets append the first file to the second and then write the second back to the first... without appending
try (BufferedReader console = new BufferedReader(new InputStreamReader(System.in))
) {
String file1 = console.readLine();
String file2 = console.readLine();
try (InputStream f1 = new BufferedInputStream(new FileInputStream(file1));
OutputStream f2 = new BufferedOutputStream(new FileOutputStream(file2, true))
) {
// append file1 to file2
while (f1.available() > 0)
f2.write(f1.read());
}
try (InputStream f2 = new BufferedInputStream(new FileInputStream(file2));
OutputStream f1 = new BufferedOutputStream(new FileOutputStream(file1))
) {
// copy file2 to file1
while (f2.available() > 0)
f1.write(f2.read());
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
}