4
In my application, I have a class that periodically performs a backup of a file, considerably small (less than 1MB), but I am doing some tests 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 until it is quite satisfactory.
Follow 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();
}
It is possible to make this copy without using buffer? Remove the buffer really optimizes file copy?
Do you want to use something ready or do the algorithm in hand?
– Maniero
@Good mustache, initially I would like to understand, even if it is in a summarized form, the functioning. Of course an example wouldn’t be all bad, even if it doesn’t have much to do with what I exemplified.
– user28595
Is that the question is not clear on this, if it is something ready, is already answered, you could put what you have already done in chat. If you want to know how to do it manually, the question doesn’t talk about it.
– Maniero
@bigown what I wanted to know is if it is possible, using inputStream and outputStream, to make the copy without having to use buffer, that code there that I showed is using filechannel.
– user28595
It is possible, as you have already read in the OS, basically have to access the DMA, I do not know how to do this in Java. But I think in this case can not use stream. I find it strange to wear a stream , if not using memory.
– Maniero
@bigown my question is this: if it was possible, and if there’s performance benefit in that.
– user28595