关闭try语句来关闭IO流
三楚雄风 2015-04-07 09:47:44 public class FileReaderTest
{
public static void main(String[] args) throws IOException
{
try(FileReader fr=new FileReader("FileReaderTest.java") )
{
char[] cbuf=new char[32];
int hasRead=0;
while((hasRead=fr.read(cbuf))>0)
{
System.out.print(new String(new String(cbuf,0,hasRead));
}
}
catch(IOException ex)
{
ex.peintStackTrace();
}
}
}
这是java讲义书上的一段代码,说可以通过自动关闭try语句来关闭IO流。求大神解释一下原理,try语句什么时候自动关闭啊?