我必须关闭 FileInputStream 吗?
我正在测试自动化领域担任实习生。 我正在使用 Eclipse 创建 Junit 代码并使用 Eclipse 运行。 我正在使用 FileInputStream 函数从 Excel 工作表中检索数据。
FileInputStream fi=new FileInputStream("c:\\search.xls");
Workbook w=Workbook.getWorkbook(fi);
Sheet s=w.getSheet(0);
是否需要关闭Inputstream功能?如果是这样,请指导我一些编码。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
是的,如果您希望释放系统资源,您需要
关闭
输入流。FileInputStream.close()< /code>
就是你所需要的。
Yes, you need to
close
the inputstream if you want your system resources released back.FileInputStream.close()
is what you need.您要么需要 close(),要么结束您的程序。
但是,如果不关闭文件,您可能会遇到令人困惑的问题,因为
最佳实践是始终关闭您用完的资源,但是我将单元测试视为脚本,并不总是必须遵循最佳实践。
You either need to close(), or end your program.
However you can run into confusing issues if you don't close the file as
It is best practice to always close your resources which you are finished with them, however I see unit tests as scripts which don't always have to follow best practice.
关闭您使用的资源始终是个好主意,但是:
如果您在资源 B 中使用资源 A,则关闭 是明智的做法B 而不是 A(如果它有方法的话)。
在您的情况下,您在
Workbook
中使用FileInputStream
,因此您最好关闭Workbook
并依赖Workbook
它将关闭FileInputStream
。在这种特殊情况下,实际上,
Workbook
将关闭< /a>FileInputStream
位于getWorkbook()
方法的末尾,但这仍然是一个好主意 关闭Workbook
以便能够进行垃圾收集。It's always a good idea to close resources you use, BUT:
If you use resource A in resource B, it's sensible to close B instead of A if it has a method for it.
In your case, you use
FileInputStream
inWorkbook
, so you'd better to closeWorkbook
and rely onWorkbook
that it will closeFileInputStream
.In this particular case, actually,
Workbook
will closeFileInputStream
at the end of thegetWorkbook()
method but it's still a good idea to closeWorkbook
to be able to be garbage collected.是的!在使用完资源后,您应该始终释放它们。 Java 有一个强大的垃圾收集机制(请注意,它与资源管理/泄漏相比是不同的。)
那么垃圾收集器无法确定您将来是否需要该资源?未能释放资源可能会导致诸如拒绝服务、性能不佳等问题。
尝试使用资源
正如已经回答的那样,但另一种更省力的方法是立即 您不需要显式调用 fi.close() 方法。
Yes! you should always release the resources once after you are done with them. Java has a powerful mechanism for Garbage Collection(note that it is different thing compare to resource management/leaks.)
So a Garbage collector can not determine that if you need the resource in future or not? Failing to release resources may cause issues like- Denial of services, poor performance .
As already answered but another effort less way is try with resources
Now you don't need to explicitly call fi.close() method.
Workbook 实现 Closeable 接口,该接口指示您应该调用 close() 方法或对资源使用 try 来释放 Workbook 对象获取的资源。
仅通过观察工作簿具有 close() 方法,您无法得出有关工作簿如何使用 InputStream 资源的任何结论。
真正发生的是工作簿构造函数使用流来初始化自身。构造函数不保留对流的引用。 Workbook 构造函数不会关闭InputStream。您有责任关闭InputStream,您可以在构造Workbook 对象后立即执行此操作。因此下面的代码是可以的:
The Workbook implements the Closeable interface which indicates that you should call the close() method or use a try with resources to free resources acquired by a Workbook object.
You can’t conclude anything on how the InputStream resource is used by the Workbook by simply observing that the Workbook has a close() method.
What really goes on is that the Workbook constructor uses the stream to initialize itself. The constructor does not keep a reference to the stream. The Workbook constructor does not close the InputStream. It is your responsibility to close the InputStream and you can do this immediately after the Workbook object is constructed. The following code is therefore OK:
最近,当我尝试重构代码时,我必须将工作簿创建移至另一个方法,并在该方法中创建 FileInputStream。该方法创建一个 FileInputStream 并返回一个工作簿。但是FileInputStream从main方法中是不可见的;那么如何在 main 方法结束时关闭 FileInputStream 呢?答案是,您不必关闭 FileInputStream,而只需关闭工作簿,工作簿会在内部关闭 FileInputStream。简而言之,无论如何都必须关闭 FileInputStream 的说法是不正确的。
Recently, when I tried to refactor my code, I had to move the workbook creation to another method, and the FileInputStream is created in that method. That method creates a FileInputStream and returns a Workbook. But FileInputStream is not visible from the main method; so how will I close my FileInputStream at the end of main method? The answer is, you don't have to close FileInputStream, instead you just close the workbook, which internally closes FileInputStream. In short, it is incorrect to say that you must close FileInputStream no matter what.
我这样做可以确保关闭excel文件输入流,这可能有帮助
9b9ea92b-5b63-47f9-a865-fd40dd602cd5
I do such a way to ensure to close the excel file input stream, this maybe helps
9b9ea92b-5b63-47f9-a865-fd40dd602cd5
做这样的事情。
Do something like this.
Basic CompSci 101 告诉我们要确保关闭用 Java 或任何语言打开的资源。所以是的,您需要关闭它们。如果你不这样做,坏的juju就一定会发生。
另外,您应该学习(并愿意)使用 Javadocs。查看 FileInputStream 和 Closeable 的 Javadoc。答案就在那里。
Basic CompSci 101 tell us to make sure to close resources that we open, in Java or any language. So yes, you need to close them. Bad juju is bound to happen when you do not do so.
Also, you should learn (and have the inclination) to use the Javadocs. Look up at the Javadoc for FileInputStream and Closeable. The answers are there.