Testing my code, it works. Unless I'm not undestanding the task as they meant it, then it should pass.
1. Catch the exception if file doesn't exist
2. Read the source file again
3. Read destination file
Its dumb to catch the exception the first time you read it and not the second one, but that is what I undestand. Anyway, the hint says to verify that the contents are copied, and they are indeed copied (tried with some local test files), so again... A tasks that cannot verify due to some not so clear requirements... I'm getting a little tired of this
package com.codegym.task.task09.task0929;
import java.io.*;
/*
Let's make the code do something useful!
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sourceFileName = reader.readLine();
String destinationFileName;
InputStream fileInputStream = null;
OutputStream fileOutputStream;
try {
fileInputStream = getInputStream(sourceFileName);
} catch (IOException e) {
System.out.println("File does not exist.");
} finally {
sourceFileName = reader.readLine();
fileInputStream = getInputStream(sourceFileName);
destinationFileName = reader.readLine();
fileOutputStream = getOutputStream(destinationFileName);
while (fileInputStream.available() > 0) {
int data = fileInputStream.read();
fileOutputStream.write(data);
}
}
fileInputStream.close();
fileOutputStream.close();
}
public static InputStream getInputStream(String fileName) throws IOException {
return new FileInputStream(fileName);
}
public static OutputStream getOutputStream(String fileName) throws IOException {
return new FileOutputStream(fileName);
}
}