Maneesh Godbole wrote:What type of file is it? Text? Binary?
Also, why are you reading one byte at one time?
I guess its binary, its an Adobe Photoshop PSD file.
I am kinda new to Java, i don't know why i read 1 byte at a time.
I thought that the BufferedInputStream() would put the bytes in a buffer, not reading the file byte by byte.
The makeBackup method (horrible name, suggestions are welcome) is supposed to take all kinds of file formats.
Kind regards Mads Nielsen
Rob Spoor wrote:
Mads Nielsen wrote:I thought that the BufferedInputStream() would put the bytes in a buffer, not reading the file byte by byte.
True, but you're still writing a byte at a time.
Great. using the example at: http://www.coderanch.com/how-to/java/AvailableDoesntDoWhatYouThinkItDoes speeded things up dramatically.
Copying a 3,42 MB (3.592.192 bytes) file with the old method took on average 127,68 seconds.
Copyting the same file with the new buffered method takes on average 0,3429 seconds.
Thank you very much.
You can still make a few improvements to your code:
1) get rid of the DataInputStream. You aren't using any of its features at all. Change line 12 to this:
2) close the InputStream as well.
3) do the closing of both in a finally block.
If you have Java 7 it will be even easier than ever:
This so-called try-with-resources block will automatically close both streams now.