Advertisement
jaVer404

level18.lesson10.home04(test_failed) line+line

Dec 14th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. package com.javarush.test.level18.lesson10.home04;
  2.  
  3. /* Объединение файлов
  4. Считать с консоли 2 имени файла
  5. В начало первого файла записать содержимое второго файла так, чтобы получилось объединение файлов
  6. Закрыть потоки. Не использовать try-with-resources
  7. */
  8.  
  9. import java.io.*;
  10.  
  11. public class Solution {
  12.     public static void main(String[] args) throws IOException {
  13.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  14.         String fileName1=reader.readLine();
  15.         String fileName2=reader.readLine();
  16.         File tempFile = File.createTempFile("temp","tmp");
  17.         reader.close();
  18.  
  19.         FileInputStream fileInputStream = new FileInputStream(fileName2);//читаем со вторго файла
  20.         FileOutputStream fileOutputStream = new FileOutputStream(tempFile,true);//пишем в temp
  21.         byte[]buffer = new byte[1024];
  22.         while (fileInputStream.available()>0) {
  23.             int count = fileInputStream.read(buffer);
  24.             fileOutputStream.write(buffer,0,count);
  25.         }
  26.         fileInputStream.close();
  27.         fileOutputStream.close();
  28.  
  29.         FileInputStream fileInputStream2 = new FileInputStream(fileName1);//читаем с первого файла
  30.         FileOutputStream fileOutputStream2 = new FileOutputStream(tempFile,true);//пишем в temp
  31.         byte[]buffer2 = new byte[1024];
  32.         while (fileInputStream2.available()>0) {
  33.             int count = fileInputStream2.read(buffer2);
  34.             fileOutputStream2.write(buffer2,0,count);
  35.         }
  36.         fileInputStream2.close();
  37.         fileOutputStream2.close();
  38.  
  39.         FileInputStream fileInputStream3 = new FileInputStream(tempFile);//читаем с temp
  40.         FileOutputStream fileOutputStream3 = new FileOutputStream(fileName1,false);//пишем в первый файл
  41.         byte[]buffer3 = new byte[1024];
  42.         while (fileInputStream3.available()>0) {
  43.             int count = fileInputStream3.read(buffer3);
  44.             fileOutputStream3.write(buffer3,0,count);
  45.         }
  46.         tempFile.delete();
  47.         fileInputStream3.close();
  48.         fileOutputStream3.close();
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement