java file关闭吗,为什么java.io.File没有close()方法?

Java的File类代表文件和目录路径名的抽象表示,它主要用于文件和目录的管理,如检查存在性,但不直接处理输入输出流。而RandomAccessFile类提供了读写文件的随机访问能力,它包含close()方法来关闭文件。File类没有close()方法是因为文件在Java中不需要显式关闭,其资源通常在垃圾回收时自动释放。然而,这种设计在某些情况下可能导致混淆。
摘要由CSDN通过智能技术生成

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?

Thanks!

解决方案

The javadoc of the File class describes the class as:

An abstract representation of file and directory pathnames.

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值