Mahin Khan
Mahin Khan

Reputation: 776

Stream Object Initialization

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?

Upvotes: 2

Views: 4420

Answers (4)

Yves_T
Yves_T

Reputation: 1170

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.

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

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:

Upvotes: 3

Yogendra Singh
Yogendra Singh

Reputation: 34367

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;

Upvotes: 0

kosa
kosa

Reputation: 66637

FileInputStream fin=null;

Assign it null or FileInputStream object.

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

Upvotes: 0

Related Questions