는 다음과 같이 거의 끝났다 :자바 스트림을 닫는 방법? 나는 자바 6 스트림을 처리 볼 때마다
public void myMethod() throws Exception
{
InputStream stream = null;
try
{
stream = connection.openConnection();
...
}
finally
{
if(stream != null)
{
stream.close();
}
}
}
그러나 나는이 필요한 이유를 실패합니다. 이 방식이 똑같지 않을까요? 그것은되지 openConnection()
하면
public void myMethod() throws Exception
{
InputStream stream = connection.openConnection();
try
{
...
}
finally
{
stream.close();
}
}
는 stream
가 할당되지 않습니다 실패하고 아무것도 어쨌든 닫 없다?
connection.openConnection()이 실패하면 예외가 발생합니다. try와 finally는 의미가 없으며 실패하지 않고 null 인'stream'이라고 가정하고 마침내 NullPointerException을 throw합니다. – nachokk
예, 그렇지만 IOException를 방아쇠하는 스트림과의 상호 작용이있는 경우, 점에 의해 감추어지는 – Taig
@nachokk하지만 메소드는 이미'Exception'을 throw합니다. 또한 아마도'stream'을 가진 어떤 연산도'Exception'을 던질 수 있습니다. –