I get all green check marks, but I hacked my way in.
How can I fix this so it actually looks like something
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;
InputStream fileInputStream =null;
try {
sourceFileName = reader.readLine();
fileInputStream = getInputStream(sourceFileName);
}
catch (FileNotFoundException e){
System.out.println("File does not exist.");
System.out.println("file was not really found so here; " + e);
System.out.println("try again to enter file name");
sourceFileName = reader.readLine();
fileInputStream = getInputStream(sourceFileName);
System.out.println("now for the output filename");
}
catch (Exception e){
System.out.println("some other exception happened");
}
String destinationFileName = reader.readLine();
OutputStream fileOutputStream = getOutputStream(destinationFileName);
while (fileInputStream.available() > 0) {
int data = fileInputStream.read();
fileOutputStream.write(data);
}
//finally{
// 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);
}
}
Kent Hervey
Level 19
This very, very ugly bad code "works"
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
10 March 2020, 19:43
0
Kent Hervey Software Engineer/Consult Expert
16 March 2020, 01:10
Thanks for the comments
0
Kent Hervey Software Engineer/Consult Expert
10 March 2020, 16:14
And why did I need the below:
InputStream fileInputStream =null;
I had just InputStream fileInputStream; but then a line in the while complained that I had not initialized fileInputStream...but I had in both/either the try or the catch
??
0
Kent Hervey Software Engineer/Consult Expert
10 March 2020, 16:13
for example, why did I need to comment out my finally? It should have worked, but IDE complained that I had a finally without a try
0