Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) throws IOException {
  6.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  7.         String in = bufferedReader.readLine();
  8.         String out = bufferedReader.readLine();
  9.         reverseByByte(in, out);
  10.     }
  11.  
  12.     private static void reverseByByte(String input, String output) {
  13.         String pathOutput = output.equals("") ? input : output;
  14.         try (RandomAccessFile rafInput = new RandomAccessFile(input, "rw");
  15.              RandomAccessFile rafOutput = pathOutput.equals(input) ?
  16.                      rafInput :
  17.                      new RandomAccessFile(pathOutput, "rw")) {
  18.  
  19.             System.out.println("Имя входного файла: " + input + "\nИмя выходного файла:" + pathOutput + "\nТип разворота: побайтовый");
  20.  
  21.             if (rafInput == rafOutput)
  22.                 reverseByByteInTheSameFile(rafInput);
  23.             else {
  24.                 reverseByByteInTheOtherFile(rafInput, rafOutput);
  25.             }
  26.  
  27.         } catch (IOException e) {
  28.             e.printStackTrace();
  29.         }
  30.     }
  31.  
  32.     private static void reverseByByteInTheSameFile(RandomAccessFile raf) throws IOException {
  33.         byte temp1;
  34.         byte temp2;
  35.         for (int i = 0; i < raf.length() / 2; i++) {
  36.             raf.seek(i);
  37.             temp1 = raf.readByte();
  38.  
  39.             raf.seek(raf.length() - i - 1);
  40.             temp2 = raf.readByte();
  41.  
  42.             raf.seek(raf.length() - i - 1);
  43.             raf.writeByte(temp1);
  44.  
  45.             raf.seek(i);
  46.             raf.writeByte(temp2);
  47.         }
  48.     }
  49.  
  50.     private static void reverseByByteInTheOtherFile(RandomAccessFile rafInput, RandomAccessFile rafOutput) throws IOException {
  51.         byte temp;
  52.         for (int i = (int) rafInput.length() - 1; i >= 0; i--) {
  53.             rafInput.seek(i);
  54.             temp = rafInput.readByte();
  55.             rafOutput.writeByte(temp);
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement