2

Now I am getting compile time error at line 30 and 38 that 'fin' might not have been initialized. but its perfectly to write it this way

 import java.io.*;
    class CopyFile {
        public static void main(String args[]) throws IOException {
            int i;
            FileInputStream fin;//can't it be done like this?
            FileOutputStream fout= new FileOutputStream(args[1]);

            try{
                //open input file
                try{
                    fin = new FileInputStream(args[0]);
                }
                catch(FileNotFoundException e){
                    System.out.println("Input file Not Found");
                    return;
                }
                //open output file
                try{
                    fout = new FileOutputStream(args[1]);
                }
                catch(FileNotFoundException e){
                    System.out.println("Error Opening File");
                }
            }
            catch(ArrayIndexOutOfBoundsException e){
                System.out.println("usage: Copyfile From to");
            }
            try{
                do{
                    i = fin.read();
                    if(i!= -1)
                        fout.write(i);
                }while(i != -1);
            }
            catch(IOException e){
                System.out.println("file error");
            }
            fin.close();
            fout.close();
        }
    }

I have seen it many time initialized like this. I think its due to the try blocks.

it might miss the initialization due to being in the try block and hence the error?

Mahin Khan
  • 776
  • 2
  • 11
  • 19

4 Answers4

3

The problem is that you're not initializing the FileInputStream fin at all. Your code will look like this to the compiler:

FileInputStream fin;
try {
    fin = ...
    //more code goes here...
} catch (...) {
    //exception handling...
} finally {
    fin.close(); //fin is not even null for the compiler
}

In order to make the code work, initialize it at least with a null value and check if fin != null before using the close method.

FileInputStream fin = null;
try {
    fin = ...
    //more code goes here...
} catch (...) {
    //exception handling...
} finally {
    if (fin != null) {
        fin.close(); //fin is not null, at least the JVM could close it
    }
}

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • if the try block fails then 'fin' has not been initialized at all and then JVM will have trouble closing it right? – Mahin Khan Nov 11 '12 at 04:28
  • It will have problems using an object that points to no reference at all. That's why the compiler mark that lines as an error. It's a good practice to initialize your variables before using them. – Luiggi Mendoza Nov 11 '12 at 04:30
0

FileInputStream fin=null;

Assign it null or FileInputStream object.

Local variable need to be assigned to some value before being used.

kosa
  • 65,990
  • 13
  • 130
  • 167
0

Though in the first try block, you are initializing fin as fin = new FileInputStream(args[0]);, your nested statements confuse the compiler. Just update your declaration as below:

      FileInputStream fin = null;
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • yes I know ways to make it work :) but it works just fine in other programs I have seen in books and stuff. – Mahin Khan Nov 11 '12 at 04:25
  • @user1815144: Its just because of your nested `try` blocks confusing the compiler. It think if it doesn't go inside those blocks, `fin` may remain uninitialized and hence complaining. If you reduce the nesting, I am sure it should work here too. – Yogendra Singh Nov 11 '12 at 04:28
0

Dont use try catch for an if and vice versa.

Try/catch is for when things go wrong behind your control and that is no part of normal program flow for example writing to a hard disk that is full....

Use if for normal error checking

In your example check your args array with an if block and then initialize your fin.

Yves_T
  • 1,170
  • 2
  • 10
  • 16