| Author |
Speeding up BufferedInputStream and FileOutputStream
|
Mads Nielsen
Greenhorn
Joined: Aug 13, 2011
Posts: 28
|
|
Hello
running the above code on a ~3MB file takes on average about 1-2 minutes.
How do i make it faster ?
Kind regards Mads Nielsen
|
 |
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8430
|
|
What type of file is it? Text? Binary?
Also, why are you reading one byte at one time?
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Mads Nielsen
Greenhorn
Joined: Aug 13, 2011
Posts: 28
|
|
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
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
Either read byte[] by byte[], or use a BufferedOutputStream.
Also, http://www.coderanch.com/how-to/java/AvailableDoesntDoWhatYouThinkItDoes
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
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.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3044
|
|
The FileChannel class allows you to copy files at the operating system level. Check out the transferTo and transferFrom methods.
[edit]
Oh, and an even easier solution is to use the Files.copy() method.
|
 |
Mads Nielsen
Greenhorn
Joined: Aug 13, 2011
Posts: 28
|
|
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.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
You're welcome.
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.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
|
There are better and easier ways to copy a file. In my answer to your previous post I posted a link to a page with examples that show how to copy a file.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: Speeding up BufferedInputStream and FileOutputStream
|
|
|