Re: Copy files in java gives me out of memory error



Daniel Pitts schreef:
Use a FileInputStream, and a FileOutputStream, and copy a small amount
at a time (say 8k) using a byte array.

Thanks its working now, using a method to create backup instead of
another class. Anyway, while working with FileOutputStream I've noticed
that if Im doing this then it just run,

FileInputStream from = new FileInputStream(fromFile);
FileOutputStream to = new FileOutputStream(toFile);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesWrite;

while ((bytesWrite = from.read(buffer)) != -1) {
to.write(buffer, 0, bytesWrite);
}

but if I'm assigning "from.read(buffer)" to byteWrite first, then the
apps freeze. Why can't I do this?

FileInputStream from = new FileInputStream(fromFile);
FileOutputStream to = new FileOutputStream(toFile);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesWrite = from.read(buffer);

while (bytesWrite != -1) {
to.write(buffer, 0, bytesWrite);
}
.