• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Different size when copy a file with FileIOStream

 
Karim Kiswarday
Ranch Hand
Posts: 50
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Karim Kiswarday
Ranch Hand
Posts: 50
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect



Thanks
KK
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic