为什么 java.io.File 没有 close 方法? [英] Why doesn't java.io.File have a close method?

查看:28
本文介绍了为什么 java.io.File 没有 close 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然 java.io.RandomAccessFile 确实有一个 close() 方法 java.io.File 没有.这是为什么?文件是在完成时自动关闭还是什么?

While java.io.RandomAccessFile does have a close() method java.io.File doesn't. Why is that? Is the file closed automatically on finalization or something?

推荐答案

File 类的 javadoc 将该类描述为:

The javadoc of the File class describes the class as:

文件和目录路径名的抽象表示.

An abstract representation of file and directory pathnames.

File 只是一个路径名的表示,有一些关于文件系统的方法(比如 exists())和目录处理,但实际的流输入和输出已经完成别处.流可以打开和关闭,文件不能.

File is only a representation of a pathname, with a few methods concerning the filesystem (like exists()) and directory handling but actual streaming input and output is done elsewhere. Streams can be opened and closed, files cannot.

(我个人的看法是,Sun 继续创建 RandomAccessFile 是相当不幸的,因为它的命名不一致导致了很多混乱.)

(My personal opinion is that it's rather unfortunate that Sun then went on to create RandomAccessFile, causing much confusion with its inconsistent naming.)

这篇关于为什么 java.io.File 没有 close 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆