Created
April 6, 2015 18:52
-
-
Save shcherbakoff/3a6e744d035db885917b to your computer and use it in GitHub Desktop.
package com.javarush.test.level09.lesson11.bonus02;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level09.lesson11.bonus02; | |
import java.io.*; | |
/* Нужно добавить в программу новую функциональность | |
Задача: Программа вводит два имени файла. И копирует первый файл на место, заданное вторым именем. | |
Новая задача: Программа вводит два имени файла. И копирует первый файл на место, заданное вторым именем. | |
Если файла (который нужно копировать) с указанным именем не существует, то | |
программа должна вывести надпись «Файл не существует.» и еще раз прочитать имя файла с консоли, а уже потом считывать файл для записи. | |
*/ | |
public class Solution | |
{ | |
public static void main(String[] args) throws IOException | |
{ | |
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
String sourceFileName = reader.readLine(); | |
FileInputStream fileInputStream = null; | |
try | |
{ | |
fileInputStream = new FileInputStream(sourceFileName); | |
} | |
catch (FileNotFoundException e) | |
{ | |
System.out.println("Файл не существует."); | |
sourceFileName = reader.readLine(); | |
fileInputStream = new FileInputStream(sourceFileName); | |
} | |
String destinationFileName = reader.readLine(); | |
FileOutputStream fileOutputStream = new FileOutputStream(destinationFileName); | |
while (fileInputStream.available() > 0) | |
{ | |
int data = fileInputStream.read(); | |
fileOutputStream.write(data); | |
} | |
fileInputStream.close(); | |
fileOutputStream.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment