Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
- String in = bufferedReader.readLine();
- String out = bufferedReader.readLine();
- reverseByByte(in, out);
- }
- private static void reverseByByte(String input, String output) {
- String pathOutput = output.equals("") ? input : output;
- try (RandomAccessFile rafInput = new RandomAccessFile(input, "rw");
- RandomAccessFile rafOutput = pathOutput.equals(input) ?
- rafInput :
- new RandomAccessFile(pathOutput, "rw")) {
- System.out.println("Имя входного файла: " + input + "\nИмя выходного файла:" + pathOutput + "\nТип разворота: побайтовый");
- if (rafInput == rafOutput)
- reverseByByteInTheSameFile(rafInput);
- else {
- reverseByByteInTheOtherFile(rafInput, rafOutput);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void reverseByByteInTheSameFile(RandomAccessFile raf) throws IOException {
- byte temp1;
- byte temp2;
- for (int i = 0; i < raf.length() / 2; i++) {
- raf.seek(i);
- temp1 = raf.readByte();
- raf.seek(raf.length() - i - 1);
- temp2 = raf.readByte();
- raf.seek(raf.length() - i - 1);
- raf.writeByte(temp1);
- raf.seek(i);
- raf.writeByte(temp2);
- }
- }
- private static void reverseByByteInTheOtherFile(RandomAccessFile rafInput, RandomAccessFile rafOutput) throws IOException {
- byte temp;
- for (int i = (int) rafInput.length() - 1; i >= 0; i--) {
- rafInput.seek(i);
- temp = rafInput.readByte();
- rafOutput.writeByte(temp);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement