Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level19.lesson05.task01;
- /* Четные байты
- Считать с консоли 2 имени файла.
- Вывести во второй файл все байты с четным индексом.
- Пример: второй байт, четвертый байт, шестой байт и т.д.
- Закрыть потоки ввода-вывода.
- */
- import java.io.*;
- public class Solution {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String source = reader.readLine();
- String output = reader.readLine();
- reader.close();
- FileOutputStream fos = null;
- byte[]someBytes = bytesFromFile(source);
- try
- {
- fos = new FileOutputStream(output);
- for (int i = 0; i < someBytes.length;i++) {
- if (i%2!=0) {
- fos.write(someBytes[i]);
- }
- }
- fos.close();
- }
- catch (Exception e)
- {}
- finally
- {
- if (reader!=null) {
- reader.close();
- }
- if (fos!=null) {
- fos.close();
- }
- }
- }
- public static byte[] bytesFromFile (String fileName) {
- FileInputStream fIS = null;
- byte [] bytes = null;
- try
- {
- File sourseFile = new File(fileName);
- bytes = new byte[(int)sourseFile.length()];
- fIS = new FileInputStream(sourseFile);
- fIS.read(bytes);
- fIS.close();
- }
- catch (Exception e) {
- }
- finally
- {
- if (fIS != null) {
- try
- {
- fIS.close();
- }
- catch (Exception e) {}
- }
- }
- return bytes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement