File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Different size when copy a file with FileIOStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Different size when copy a file with FileIOStream" Watch "Different size when copy a file with FileIOStream" New topic
Author

Different size when copy a file with FileIOStream

Karim Kiswarday
Ranch Hand

Joined: Oct 01, 2010
Posts: 50

Hello everybody.
I've done a program to copy a binary file in another (new) file.

This is the code (I've removed the catch block and the close methods):

I read 1024 byte (1KB) at a time from the first file and I copy it in the second file until the FileInputStream.read() method return -1.

The problem is about the size of the second file, it's not the same of the first file.

My disk partition have clusters of 4KB

The first file has a size of 52.597 byte (51.3KB) but for the clusters size it's 52KB on the disk.
The second file after I copy it has 54.272 byte (53KB).

I understand why 52.597 byte (51.3KB) are allocated in 52KB on the disk, but I DON'T understand why after the copy the file dimension is changed from 51.3KB to 53KB.

The nCopy variable tells me that the while block is executed 53 times, but I should have read the whole file after 52 iterations (52x1024=53248).

The copied file is not corrupted.

After this I have modified the array in 1

and the iterations are 52598 and file size is 52.598 byte.
It's only one byte more than original but why it doesn't stop when it arrives at the end of file?

Where I wrong?
Why my duplicated file is not corrupted when other excess bytes are copied in it?

I hope somebody can explain me.
Thanks
KK


No fun at the table, no fun in the sack.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

If you check the Java doc you will see that
returns the number of bytes actually read so you must only write that number out i.e.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Karim Kiswarday
Ranch Hand

Joined: Oct 01, 2010
Posts: 50

Perfect



Thanks
KK
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Different size when copy a file with FileIOStream