I am working as a trainee in Test Automation. I am working with creating Junit code with Eclipse and run using Eclipse. In that I am retriving the datas from excel sheet using FileInputStream function.
FileInputStream fi=new FileInputStream("c:\\search.xls"); Workbook w=Workbook.getWorkbook(fi); Sheet s=w.getSheet(0);
Is it necessary to close the Inputstream function? If it so please guide me with some codings.
close() method. After any operation to the file, we have to close that file.
resource-leak is probably more of a concern here. Handling inputstream requires OS to use its resources and if you don't free it up once you use it, you will eventually run out of resources.
No, you only need to close the outermost stream. It will delegate all the way to the wrapped streams. However, your code contains one conceptual failure, the close should happen in finally , otherwise it's never closed when the code throws an exception between opening and closing.
It's important to close any resource that you use. in. close will close BufferedReader, which in turn closes the resources that it itself uses ie. the InputStreamReader.
Yes, you need to close
the inputstream if you want your system resources released back.
FileInputStream.close()
is what you need.
FileInputStream fi=null; try { fi=new FileInputStream("c:\\search.xls"); Workbook w=Workbook.getWorkbook(fi); Sheet s=w.getSheet(0); } finally { if (fi!=null) { fi.close(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With