请教打开文件读取文件再关闭的标准代码
书上说关闭文件的代码通常放在finally块中,于是我写了如下的代码
import java.io.*;
class ExceptionTest{
public static void main( String args[ ] ){
FileInputStream fis;
try{
fis = new FileInputStream( "D:\\projects\\java\\text " );
int b;
while( (b=fis.read())!=-1 ){
System.out.print( b );
}
}catch(
FileNotFoundException e){
System.out.println( "File
NotFoundException print ");
System.out.println(e);
}catch(
IOException e){
System.out.println( "IOException print ");
System.out.println(e);
}finally{
System.out.println( "finally print ");
try{
if(fis!=null) fis.close( );
}catch(IOException e){
}
}
System.out.println( "behind try finally ");
}
}
可是编译时提示:可能尚未初始化变量fis
------解决方案--------------------FileInputStream fis = null;
改成上面