I have this piece of code in my program, where I assign 10000 as the size of the buffer. Now How can I check if the file that I am copying is less than 10000, for example 5000, than how to free the remaining 5000 back?
Thank You.
private void copyFile(File in, File out) {
var buffer = new byte[10000];
try {
FileInputStream dis = new FileInputStream(in);
FileOutputStream dos = new FileOutputStream(out);
int count;
do {
count = dis.read(buffer);
if (count != -1)
dos.write(buffer, 0, count);
} while (count != -1);
dis.close();
dos.close();
} catch (IOException e) {
program.print("Error copying file: " + e.toString() + "\n", null);
}
}
EDIT: Is there anyway, I can optimize this more?
java.nio.file.Files.copy()\$\endgroup\$