Advertisement
jaVer404

level19.lesson05.task01(done)

Feb 18th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.94 KB | None | 0 0
  1. package com.javarush.test.level19.lesson05.task01;
  2.  
  3. /* Четные байты
  4. Считать с консоли 2 имени файла.
  5. Вывести во второй файл все байты с четным индексом.
  6. Пример: второй байт, четвертый байт, шестой байт и т.д.
  7. Закрыть потоки ввода-вывода.
  8. */
  9.  
  10. import java.io.*;
  11.  
  12. public class Solution {
  13.     public static void main(String[] args) throws IOException {
  14.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  15.         String source = reader.readLine();
  16.         String output = reader.readLine();
  17.         reader.close();
  18.         FileOutputStream fos = null;
  19.         byte[]someBytes = bytesFromFile(source);
  20.         try
  21.         {
  22.             fos = new FileOutputStream(output);
  23.             for (int i = 0; i < someBytes.length;i++) {
  24.                 if (i%2!=0) {
  25.                     fos.write(someBytes[i]);
  26.                 }
  27.             }
  28.             fos.close();
  29.         }
  30.         catch (Exception e)
  31.         {}
  32.         finally
  33.         {
  34.             if (reader!=null) {
  35.                 reader.close();
  36.             }
  37.             if (fos!=null) {
  38.                 fos.close();
  39.             }
  40.         }
  41.     }
  42.     public static byte[] bytesFromFile (String fileName) {
  43.         FileInputStream fIS = null;
  44.         byte [] bytes = null;
  45.         try
  46.         {
  47.             File sourseFile = new File(fileName);
  48.             bytes = new byte[(int)sourseFile.length()];
  49.             fIS = new FileInputStream(sourseFile);
  50.             fIS.read(bytes);
  51.             fIS.close();
  52.         }
  53.         catch (Exception e) {
  54.         }
  55.         finally
  56.         {
  57.             if (fIS != null) {
  58.                 try
  59.                 {
  60.                     fIS.close();
  61.                 }
  62.                 catch (Exception e) {}
  63.             }
  64.         }
  65.         return bytes;
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement