对IO流中read(buffer)!=-1的理解

read(buffer)!=-1的理解

InputStream.read(buffer) 方法

  1. 在java中api文档有read()这几种方法
方法摘要方法作用
abstract intread()从输入流中读取数据的下一个字节
intread(byte[] b)将输入流中读取一定数量 并将其存储在缓冲区数组 b 中。
intread(byte[] b, int off, int len)将输入流中最多 len 个数据字节读入 byte 数组。

​ 2.read()==-1的含义

/** 
	* Reads the next byte of data from the input stream. The value byte is * returned as an <code>int</code> in the range <code>0</code> to * <code>255</code>. If no byte is available because the end of the stream * has been reached, the value <code>-1</code> is returned. This method * blocks until input data is available, the end of the stream is detected, * or an exception is thrown. 
	* * <p> A subclass must provide an implementation of this method.
    * * @return     the next byte of data, or <code>-1</code> if the end of the * stream is reached. 
    * @exception  IOException  if an I/O error occurs. 
*/

​ 这是read()方法中的注释,意识就是read()从输入流中读取下一个字节。如果没有字节可读(也就是read()读到文件最后了)read()返回-1.

流程:

  1. 如何判断文件读取完成?

    若我们下载一个文件,要在读取中不断的获取read()的返回值,判断何时-1,来表示读取完成。

  2. 为何不使用read(),而是用read(buffer)?

    原因是:read(buffer)效率更高,如果文件有10000byte,使用read()要读10000次然后不停的往存储上写入,而使用read(buffer) 可以读取最大buffer长度的数据(如:buffer长度1000),只需11次(为何11次而不是10,请接着看),然后写入存储中。

  3. read(buffer)如何读入?

    如10000byte的文件下载,我们buffer长度1000,read(buffer)其实可以看做,是将文件分成【(10000除以1000 向上取整)+1 份】11份,其中最后一块其实就是个空的,用来判断文件读取完成。

根据上面的源码,我们知道其实是read(buffer) 里面调用 read(byte b[], int off, int len), 在 read(byte b[], int off, int len) 中,读取文件

  • 首先

,在每个段中,使用

                int c = read();
                     if (c == -1) {
                         return -1;
                     }

判断第一个字节是否是-1. c==-1,说明当前是上面的1所指的第11块。

  • 其次

如果c!=-1,说明文件未读取完成(也就是1-10块),for循环

   for (; i < len ; i++) {
                             c = read();
                             if (c == -1) {
                                 break;
                             }
                             b[off + i] = (byte)c;

不断地读取字节,如果在读取期间c==-1.说明文件读取完成(也就是第10块,第10块写入的byte长度,不一定都填充满buffer的长度1000),此时,break,跳出循环。进行下一个read(buffer) ,此时满足上面的if,返回-1。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是对该代码进行详细注释的版本: ```java import java.io.*; public class FileMerge { public static void main(String[] args) { // 判断命令行参数是否为3个,如果不是则输出用法并返回 if (args.length != 3) { System.out.println("Usage: java FileMerge source1 source2 target"); return; } // 读取命令行参数的源文件1、源文件2和目标文件名 String sourceFile1 = args[0]; String sourceFile2 = args[1]; String targetFile = args[2]; try ( // 创建输入流1,使用缓冲区读取文件内容 BufferedInputStream in1 = new BufferedInputStream(new FileInputStream(sourceFile1)); // 创建输入流2,使用缓冲区读取文件内容 BufferedInputStream in2 = new BufferedInputStream(new FileInputStream(sourceFile2)); // 创建输出流,使用缓冲区写入文件内容 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(targetFile)) ) { byte[] buffer = new byte[1024]; // 缓冲区大小为1024字节 int bytesRead; // 每次读取的字节数 // 从输入流1读取数据,写入输出流 while ((bytesRead = in1.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); // 将读取到的字节数组写入输出流 } // 从输入流2读取数据,写入输出流 while ((bytesRead = in2.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); // 将读取到的字节数组写入输出流 } System.out.println("File merge completed."); } catch (FileNotFoundException e) { // 捕获文件未找到异常 System.err.println("File not found: " + e.getMessage()); } catch (IOException e) { // 捕获IO异常 System.err.println("IOException: " + e.getMessage()); } } } ``` 注释添加了对每个方法和变量的详细说明,以及对异常的捕获和处理进行了注释。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值