Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Speeding up BufferedInputStream and FileOutputStream

 
Mads Nielsen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11185
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What type of file is it? Text? Binary?
Also, why are you reading one byte at one time?
 
Mads Nielsen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either read byte[] by byte[], or use a BufferedOutputStream.

Also, http://www.coderanch.com/how-to/java/AvailableDoesntDoWhatYouThinkItDoes
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6337
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15492
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic