首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请问打开文件读取文件再关闭的标准代码

2012-01-16 
请教打开文件读取文件再关闭的标准代码书上说关闭文件的代码通常放在finally块中,于是我写了如下的代码imp

请教打开文件读取文件再关闭的标准代码
书上说关闭文件的代码通常放在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( "FileNotFoundException   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;

改成上面

热点排行