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.
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:
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:
"I'm not back." - Bill Harding, Twister
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