Does someone know why the same code works in the previous task but does not work in this one as it is the exact same task, same file only now in the form of a try-catch format. Maybe someone can explain the difference? Very much appreciated!!
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));
InputStream fileInputStream = null;
//OutputStream fileOutputStream = null;
try {
String sourceFileName = reader.readLine();//first time it reads this file
//String destinationFileName = reader.readLine();// should read the data from keyboard for this file onely once
fileInputStream = getInputStream(sourceFileName);
//fileOutputStream = getOutputStream(destinationFileName);
} catch (FileNotFoundException e){
System.out.println("File does not exist");//The main method must handle exceptions thrown by the getInputStream method. If an exception occurs, you should display "File does not exist.".
//String sourceFileName = reader.readLine();// why repeat this as the sourcefile does not exist
//fileInputStream = getInputStream(sourceFileName);
}
String sourceFileName = reader.readLine();//second time it read sthis file
String destinationFileName = reader.readLine();// reads data from keyboard for first time for this file
fileInputStream = getInputStream(sourceFileName);
OutputStream fileOutputStream = getOutputStream(destinationFileName);
while (fileInputStream.available() > 0) {//The program must copy the contents of the first file to the second one.
int data = fileInputStream.read();
fileOutputStream.write(data);
}
fileInputStream.close();
fileOutputStream.close();
}
/*public class Solution { This code works including copying from one file to another
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String sourceFileName = reader.readLine();//reads data from keyboard line by line
String destinationFileName = reader.readLine();//idem
InputStream fileInputStream = getInputStream(sourceFileName);// it calls the method getInputStream with parameter the sourceFileName
OutputStream fileOutputStream = getOutputStream(destinationFileName);// it call the method getOutputStream with parameter destinationFileName
//The program must copy the contents of the first file to the second one.
int count = 0;
while (fileInputStream.available() > 0) //as long as there are filenames in the fileInputSTream
{
int data = fileInputStream.read();//read the file inputstream
fileOutputStream.write(data);//write the date to the outputstream
count++;//counts the number of bytes copied??
}
System.out.println("Bytes copied: " + count);
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);
}
}