KristijanT

OS.1.2. ReverseByteStream

Mar 6th, 2018
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. //OS_HW01_2 161514 17/18
  2.  
  3. import java.io.*;
  4.  
  5. public class OS_HW01_2 {
  6.     public static void main(String[] args) throws IOException {
  7. //        File dir = new File(System.getProperty("user.dir"));
  8.         String pathRead = "src/texts/source.txt";
  9.         String pathWrite = "src/texts/destination.txt";
  10. //        File source = new File(pathRead);
  11. //        source.mkdirs();      //this makes a dir, not a txt file
  12. //        File destination = new File(pathWrite);
  13. //        destination.mkdirs(); //this makes a dir, not a txt file
  14.  
  15.         BufferedInputStream in = new BufferedInputStream(new FileInputStream(pathRead));
  16.         PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(pathWrite)));
  17.  
  18.         copyStreamReversed(in, out);
  19.     }
  20.     public static void copyStreamReversed(InputStream in, OutputStream out)
  21.             throws IOException {
  22.         try {
  23. //            int c;
  24.             byte[] buffer = new byte[100];
  25. //            while ((c = in.read()) != -1)
  26. //                ;
  27.             in.read(buffer);
  28.             reverse(buffer);
  29.             out.write(buffer);  //ask why?
  30.         } finally {
  31.             if (in != null) {
  32.                 in.close();
  33.             }
  34.             if (out != null) {
  35.                 out.close();
  36.             }
  37.         }
  38.     }
  39.     public static void reverse(byte[] arr) {
  40.         if (arr == null) {
  41.             return;
  42.         }
  43.         int i = 0;
  44.         int j = arr.length - 1;
  45.         byte tmp;
  46.         while (j > i) {
  47.             tmp = arr[j];
  48.             arr[j] = arr[i];
  49.             arr[i] = tmp;
  50.             j--;
  51.             i++;
  52.         }
  53.     }
  54. }
Add Comment
Please, Sign In to add comment