• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Speeding up BufferedInputStream and FileOutputStream

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either read byte[] by byte[], or use a BufferedOutputStream.

Also, https://coderanch.com/how-to/java/AvailableDoesntDoWhatYouThinkItDoes
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: https://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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic