注册 登录
编程论坛 JAVA论坛

while((remain = fileInputStream.read()) != -1)

yu1543054075 发布于 2015-06-14 20:55, 451 次点击
import *;
public class FileStreamDemo {
    public static void main(String[] args) {
        try {
            byte[] buffer = new byte[1024];
            FileInputStream fileInputStream =
            new FileInputStream(new File(args[0]));
            FileOutputStream fileOutputStream =
            new FileOutputStream(new File(args[1]));
            System.out.println("复制档案:" +
            fileInputStream.available() + "字节");
            while(true) { // 从来源档案读取数据至缓冲区
                if(fileInputStream.available() < 1024) {
                    int remain;
                    while((remain = fileInputStream.read())
                    != -1) {
                        fileOutputStream.write(remain);
                    }
                    break;
                }
                else {
                    fileInputStream.read(buffer);
                    // 将数组数据写入目的档案
                    fileOutputStream.write(buffer);
                }
            }
            // 关闭串流
            fileInputStream.close();
            fileOutputStream.close();
            System.out.println("复制完成");
        }
        catch(ArrayIndexOutOfBoundsException e) {
            System.out.println(
            "using: java FileStreamDemo src des");
            e.printStackTrace();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }
}
 int remain;
while((remain = fileInputStream.read())!= -1)
不明白是什么意思,求指导!!!
3 回复
#2
秦时的明月夜2015-06-15 07:53
文件读取……=-1,就是读到文件结束了……没有就是继续……
#3
日知己所无2015-06-19 22:34
FileInputStream.read()的API文档里是这么说的:

Returns:the next byte of data, or -1 if the end of the file is reached.
返回值:下一个字节的数据,或者-1(代表文件结束了)

http://docs.
#4
日知己所无2015-06-19 22:40
在官网上,找到了中文的API说明

返回:下一个数据字节;如果已到达文件末尾,则返回 -1。

http://download.()
1