Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Why picture is not being copy

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a code which read the byte from C://A//1.bmp and write it to C://B//1.bmp using DataInput/OutputStreams.
But when I check C://B//1.bmp the size of bmp is same but there is black out.
Why? Please tell me the solution.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's see the code you use to create Input/Output Streams and copy from one to another.
 
Huzefa Zohaib
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank's Jim
Here is the code
 
Huzefa Zohaib
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank's Jim
Here is the code
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the biggest problem is here:

According to the API for readByte(), it can return a value from -128 to 127. The only way it indicates the end of input is by throwing EOFException. So most likely, your file has a -1 somewhere; the loop stops with the remainder of the buffer set to zeros, and then the entire buffer (including who-knows-how-many zeros) is written to the file. So using readByte() is no good here.
Another problem is the use of available() to size the buffer. There's no guarantee that available() will return all bytes that will eventually be available from the file; it just tells you how many are available right now. This might be limited because (a) there's a device buffer somewhere that is full; you can't read the rest of the file until you clear some of the buffer, or (b) the file is fragmented and it's going to take a few milliseconds for the disk reader to get to the next section of the file - so available() just reports what's ready now. Possibly there are other reasons available() is limited, but those two are enough.
Also, reading one byte at a time is very inefficient and slow - you might as well grab a bunch at once.
While we're at it, there's really no use for the DataInput and Outpupt streams at all. Their methods are completely redundant for simple bulk transfer. You can just do something like this:
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, I almost overlooked this:

This code is EEEVIL and should never, ever be used. You're basically saying "if there's a problem, just ignore it, don't tell anyone, and keep it secret." This makes it far more difficult to detect and fix a problem when it occurs. At a minimum, if you're not sure what else to do, print a stack trace:

In a more advanced program you may have error logging to a file somewhere, or there may be a particular response that is appropriate based on what the program is supposed to do. But never, ever leave the catch block blank, unless you are absolutely sure you know why the exception was thrown, and that it's OK to ignore. The typical example of this is an InterruptedException:
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic