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 一个数组

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







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

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