我是否需要关闭我执行File.getName()的文件?

我将在目录中拥有大量文件。 我将使用File.getName()获取文件名,并将它们记录到日志文件中。 我认为,我不需要关闭文件,因为我没有在其中进行任何读/写操作。

它是否正确?

您永远不必关闭File ,因为它基本上是路径的表示。 只有流和读者/作家。 实际上, File甚至没有close()方法。

 Only resources needed to be close. 

在Java API中有一个接口可关闭接口 ,这些类实现了这个接口,它们需要在使用后关闭。

 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.close()方法。

对,那是正确的。 通过创建FileInputStreamFileOutputStream打开文件时,必须在最后关闭流。