java 中File.length() 和InputStream.avalible()

本文介绍了如何使用File.length()获取文件的字节长度,并解释了该方法在文件不存在时的返回值。此外,还讨论了InputStream.available()方法,它可以估算在不阻塞等待更多输入的情况下可以读取或跳过的字节数。需要注意的是,available()方法的返回值不应直接用于容器大小的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. File.length()

Returns the length of this file in bytes. Returns 0 if the file does not exist. The result for a directory is not defined.

2. InputStream.avalible()

Returns an estimated number of bytes that can be read or skipped without blocking for more input.

..It is particularly important to realize that you must not use this method to size a container and assume that you can read the entirety of the stream without needing to resize the container.

..if you're reading from a file, length() returns the current length of the file (though assuming the file's length can't change may be incorrect, reading a file is inherently racy).


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值