aspose file tools*
The moose likes Beginning Java and the fly likes Speeding up BufferedInputStream and FileOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Speeding up BufferedInputStream and FileOutputStream" Watch "Speeding up BufferedInputStream and FileOutputStream" New topic
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: 10376
    
    8

What type of file is it? Text? Binary?
Also, why are you reading one byte at one time?


[How to ask questions] [Donate a pint, save a life!] [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: 19696
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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: 3647
    
  16

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: 19696
    
  20

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

Joined: Aug 16, 2005
Posts: 14153
    
  18

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
 
jQuery in Action, 2nd edition
 
subject: Speeding up BufferedInputStream and FileOutputStream