Dear All,
Hey,
Guys, I am a bit confused about these two codes. The first one does not compile, giving me the following compile time errors:
************ CODE ONE ****************
import java.io.*;
public class iokam6
{
public iokam6(
String s1, String s2)
{
FileInputStream fin;
FileOutputStream fout;
try
{
fin=new FileInputStream(s1);
fout=new FileOutputStream(s2);
}
catch(Exception ex1)
{
System.out.println("Exception ex1 : "+ex1);
}
try
{
int i;
i=fin.read();
while(i!=-1)
{
fout.write(i);
i=fin.read();
}
}
catch(Exception ex2)
{
System.out.println("Exception ex2 : "+ex2);
}
try
{
fin.close();
fout.close();
}
catch(Exception ex3)
{
System.out.println("Exception ex3 : "+ex3);
}
}
public static void main(String args[])
{
new iokam6(args[0], args[1]);
}
}
*************************************************************************
The Errors Generated :
Variable fin may not have been initialized.i=fin.read();
Variable fout may not have been initialized.fout.write(i);
Variable fin may not have been initialized.fin.close();
Variable fout may not have been initialized.fout.close();
***********************************************************************
But the Following code compiles and runs.
******************** CODE TWO **********************
import java.io.*;
public class iokam6
{
public iokam6(String s1, String s2)
{
try
{
FileInputStream fin;
FileOutputStream fout;
fin=new FileInputStream(s1);
fout=new FileOutputStream(s2);
int i;
i=fin.read();
while(i!=-1)
{
fout.write(i);
i=fin.read();
}
fin.close();
fout.close();
}
catch(Exception ex2)
{
System.out.println("Exception ex2 : "+ex2);
}
}
public static void main(String args[])
{
new iokam6(args[0], args[1]);
}
}
*********************************************************
The only Difference between the two codes is using multiple try catch sets (as in the first code) instead of using a single try catch block (as in the later code).
Can anyone explain what is actually happening here???
Bye,
Tualha Khan