Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileOutput Stream

Status
Not open for further replies.

munnanext

Programmer
Aug 20, 2004
49
US
I am getting the following error while compiling the program
variable fos1 might not have been initialized

The code for the program is as follows:
package MyInputOutput;
import java.io.*;

public class CreateFile {

public static void main(String args[]) throws IOException{
FileOutputStream fos1;
char c;
String FilePath;
FilePath = "C:/Vishnu/Java Learning/Examples/MyInputOutput/";
FilePath = FilePath + args[0];
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
try{
fos1 = new FileOutputStream(FilePath);

}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array index out of bounds");
}
do{
c = (char) obj.read();
if (c != 'z') {
fos1.write((int) c);
}
}while(c != 'z');
fos1.close();

Here I have declared fos1, but it is giving an error that
fos1 might not have been initialized.
How can i initialize fos1?
Appreciate your time.
thanks
 
sedj is correct -- because the try..catch block introduces a new scope level, when you get to the [tt]fos.close[/tt] statement, the compiler thinks you haven't initialized it.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top