In my application, I have a class that periodically backs up a pretty small (less than 1MB) file, but I'm doing some testing and after reading this answer , it seems to me that it has been suggested that you can make copies without having to use buffer . With the code below, I'm using buffer of 4kb, and the result is quite satisfactory.
Here is the code I'm using:
private static void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
byte[] buf = new byte[4096];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
Is it possible to copy without using buffer ? Does removing the buffer really optimize the copy of files?