Методи, начебто, ті що вимагають в умові. Чи я не розумію, які треба. Потоки в блоці try-with-resources...
package ua.javarush.task.pro.task15.task1504;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
/*
Переплутані байти
*/
public class Solution {
public static void main(String[] args) {
Path path1;
Path path2;
try (Scanner scanner = new Scanner(System.in)) {
path1 = Path.of(scanner.nextLine());
path2 = Path.of(scanner.nextLine());
}
try (InputStream input = Files.newInputStream(path1);
OutputStream output = Files.newOutputStream(path2)) {
byte[] buffer = input.readAllBytes();
if(buffer.length % 2 == 1){
for(int i = 0; i < buffer.length - 1; i += 2) {
output.write(buffer[i + 1]);
output.write(buffer[i]);
}
output.write(buffer[buffer.length - 1]);
} else {
for(int i = 0; i < buffer.length; i += 2) {
output.write(buffer[i + 1]);
output.write(buffer[i]);
}
}
}catch (IOException exception){
System.out.println("Щось пішло не так..." + exception);
}
}
}