我试图创建一个备份程序来使用。我可以备份小文件,但只要我尝试备份任何大文件,就会得到ArrayIndexOutOfBoundsException。Java传输文件arrayindexoutofboundsexception
FileOutputStream fos = new FileOutputStream(dp.getPath() + ".jbackup");
byte[] buffer = new byte[4096];
int fileSize = (int)f.length();
int read = 0;
int remaining = fileSize;
while((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
remaining -= read;
fos.write(buffer, 0, read);
}
有什么建议吗?
这是功课吗?如果没有,你应该使用类似['IOUtils']的库(https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/IOUtils.html)任务。 –
不,但我通常会做所有没有库的东西(不包括反射,javassist) –
嗯,我认为这个问题本身就表明也许你不应该这样做。看看'IOUtils'中的各种'copy()'方法;你可以在一行中做到这一点,并保证工作。 –