JAVA学习日志:FileInputStream的read()读取为顺序读取

本文介绍了如何使用Java中的FileInputStream进行文件的顺序读取。通过示例代码展示了如何逐次读取文件中的字节,并解释了read()方法的工作原理。read()方法会从输入流中读取下一个字节的数据,返回的是0到255范围内的int值。当到达文件末尾时,返回-1。

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

调用FileInputStream中的read()方法时,read()从输入流中读取的字节是按顺序读取的,并且只读一遍,比如下面的示例代码中,"test.txt"文件里有"abcdef",每次读取3个字符,则第一次读取的为abc,第二次读取的为def。


示例代码:

FileInputStream fis=new FileInputStream("test.txt");

byte[] b=new byte[10];

while(true){

fis.read(b,0,3);

}



至于为什么顺序读取,看它继承的父类inputStream中的方法read()说明中有写道。


public abstract int read()

                  throws IOException


Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值