Usually, when the code needs some resource that needs to be released, I see how this is done:
InputStream in = null;
try{
in = new FileInputStream("myfile.txt");
doSomethingWithStream(in);
}finally{
if(in != null){
in.close();
}
}
What I do not like is that you must initialize the variable to zero and after that set it to a different value, and in the finally block check whether the resource has been initialized by checking whether it is zero. If it is not null, it must be freed. I know that I am bullying, but I feel that it can be done cleaner.
What I would like to do is:
InputStream in = new FileInputStream("myfile.txt");
try{
doSomethingWithStream(in);
}finally{
in.close();
}
, . , , ( ), try. , , - - ( Java), ?
:
Inputstream in = new FileInputStream("myfile.txt");
in.close();
, try-finally ?
Edit:
, , . . , - -, , , doSomethingWithStream . . , , . , ( Thread.stop), , , , .