Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level18.lesson10.home04;
- /* Объединение файлов
- Считать с консоли 2 имени файла
- В начало первого файла записать содержимое второго файла так, чтобы получилось объединение файлов
- Закрыть потоки. Не использовать try-with-resources
- */
- import java.io.*;
- public class Solution {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String fileName1=reader.readLine();
- String fileName2=reader.readLine();
- File tempFile = File.createTempFile("temp","tmp");
- reader.close();
- FileInputStream fileInputStream = new FileInputStream(fileName2);//читаем со вторго файла
- FileOutputStream fileOutputStream = new FileOutputStream(tempFile,true);//пишем в temp
- byte[]buffer = new byte[1024];
- while (fileInputStream.available()>0) {
- int count = fileInputStream.read(buffer);
- fileOutputStream.write(buffer,0,count);
- }
- fileInputStream.close();
- fileOutputStream.close();
- FileInputStream fileInputStream2 = new FileInputStream(fileName1);//читаем с первого файла
- FileOutputStream fileOutputStream2 = new FileOutputStream(tempFile,true);//пишем в temp
- byte[]buffer2 = new byte[1024];
- while (fileInputStream2.available()>0) {
- int count = fileInputStream2.read(buffer2);
- fileOutputStream2.write(buffer2,0,count);
- }
- fileInputStream2.close();
- fileOutputStream2.close();
- FileInputStream fileInputStream3 = new FileInputStream(tempFile);//читаем с temp
- FileOutputStream fileOutputStream3 = new FileOutputStream(fileName1,false);//пишем в первый файл
- byte[]buffer3 = new byte[1024];
- while (fileInputStream3.available()>0) {
- int count = fileInputStream3.read(buffer3);
- fileOutputStream3.write(buffer3,0,count);
- }
- tempFile.delete();
- fileInputStream3.close();
- fileOutputStream3.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement