banner
指数爆炸

指数爆炸

我做了对饭 !
github
bilibili

java.lang.ArrayIndexOutOfBoundsException: arraycopy: length -1 is negative

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: arraycopy: length -1 is negative
    at java.base/java.lang.System.arraycopy(Native Method)
    at java.base/java.io.BufferedOutputStream.write(BufferedOutputStream.java:129)
    at itheima11.copyfolders.copyFile(copyfolders.java:52)
    at itheima11.copyfolders.copy(copyfolders.java:31)
    at itheima11.copyfolders.copy(copyfolders.java:25)
    at itheima11.copyfolders.copy(copyfolders.java:25)
    at itheima11.copyfolders.copy(copyfolders.java:25)
    at itheima11.copyfolders.main(copyfolders.java:13)


这是我在 IO 流复制多级文件夹时遇到的问题,各种调试过后发现原因是某一个.txt 文件为空

因为那个.txt 文件为空

当执行这条语句时

int length = bfdips.read(b1);

会让 length 为 - 1

再执行这条语句时

bfdops.write(b1, 0, length);

从 0 开始,-1 的长度很明显出错了







解决办法:

第一种的时候加上判断条件,如果 length 为 - 1 的话就不进去

第二种,write 的时候,不要 read 和 write 一个数组。而是一个字节一个字节的 read 和 write

read 和 write 一个数组

    byte b1[] = new byte[10241024];
    int length = bfdips.read(b1);
    bfdops.write(b1, 0, length);

一个字节一个字节的 read 和 write

    while (true) {
        int i = bfdips.read();
        if (i != -1) {
            bfdops.write(i);
        } else {
            break;
        }
    }






以下是我自己遇到问题的代码:

package itheima11;

import java.io.*;

//把demo里的所有文件复制到demo2里面去

public class copyfolders {
    public static void main(String[] args) throws IOException {
        File f1 = new File("E:\\文档\\demo");
        File f2 = new File("E:\\文档\\demo2");

        copy(f1, f2);
    }

    public static void copy(File f1, File f2) throws IOException {
        if (f1.exists()) {
            if (f1.isDirectory()) {              //如果是目录
                String s1 = f1.getName();      //获取这个文件夹的名字
                File f3 = new File(f2, s1);
                f3.mkdir();               //在目的地创建这个文件夹

                File farr[] = f1.listFiles();
                for (File f : farr) {
                    copy(f, f3);           //递归
                }
            } else if (f1.isFile()) {          //如果是文件
                //E:\文档\demo\wenjina\xiaowenjianjia\\123.txt
                String s1 = f1.getName();
                File f4 = new File(f2, s1);     //终点文件名
                copyFile(f1, f4);
            }
        }
    }

    public static void copyFile(File start, File des) throws IOException {
        BufferedInputStream bfdips = new BufferedInputStream(new FileInputStream(start));
        BufferedOutputStream bfdops = new BufferedOutputStream(new FileOutputStream(des));

//        while (true) {
//            int i = bfdips.read();
//            if (i != -1) {
//                bfdops.write(i);
//            } else {
//                break;
//            }
//        }

        byte b1[] = new byte[10241024];
        int length = bfdips.read(b1);

        bfdops.write(b1, 0, length);

        bfdips.close();
        bfdops.close();

    }
}

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.