Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否需要关闭对其执行File.getName()操作的文件?_Java_File - Fatal编程技术网

Java 是否需要关闭对其执行File.getName()操作的文件?

Java 是否需要关闭对其执行File.getName()操作的文件?,java,file,Java,File,我会在一个目录中有很多文件。我将使用file.getName()获取文件名,并将它们记录到日志文件中。我想,我不需要关闭该文件,因为我没有在其中执行任何读/写操作 这是否正确?您永远不必关闭文件,因为它基本上是路径的表示。仅限流媒体和读者/作者。事实上,文件甚至没有close()方法。这是正确的。请注意,没有File.close()方法。是的,这是正确的。通过创建FileInputStream或FileOutputStream打开文件时,必须在最后关闭流 Only resources neede

我会在一个目录中有很多文件。我将使用
file.getName()
获取文件名,并将它们记录到日志文件中。我想,我不需要关闭该文件,因为我没有在其中执行任何读/写操作


这是否正确?

您永远不必关闭
文件,因为它基本上是路径的表示。仅限流媒体和读者/作者。事实上,
文件
甚至没有
close()
方法。

这是正确的。请注意,没有
File.close()
方法。

是的,这是正确的。通过创建
FileInputStream
FileOutputStream
打开文件时,必须在最后关闭流

Only resources needed to be close.
在JavaAPI中有一个接口,这些类实现了这个接口,它们需要在使用后关闭

close() //method is in that interface..  
使用close

It closes the stream and releases any system resources associated with it. 
If the stream is already closed then invoking this method has no effect.

File
无需关闭

如果要列出目录中的文件,可以使用如果打开一个文件,则需要关闭它,但是
File
类不表示打开的文件,它只表示一个路径。明白了。谢谢你们的回复,谢谢。如果答案对你有帮助,请投票,更重要的是,接受最好的答案。