| Author |
Copying files corrupts file
|
Kevin P Smith
Ranch Hand
Joined: Feb 18, 2005
Posts: 362
|
|
Hi guys, Have a little problem copying files using Java... I can copy the file OK, and all appears well, until you try to open the file. Then no matter what the file type (although mine are primarily images) you get the following error:- XXX can not read this file This is not a valid XXX file or it's format is not curremtly supported Here is my code (Note the DestinationFolder, DestinationFile, SourceFile etc are all valid, i just haven't showed how they are parsed as it is too long):- DestinationFolder.mkdir(); Destination.createNewFile(); InputStream input = new FileInputStream(SourceFile); OutputStream output = new FileOutputStream(DestinationFile); byte[] buffer = new byte[1024]; int length = input.read(); while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } input.close(); output.close();
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
Right here, int length = input.read(); you read the first byte of the file and discard it. If you replace this with just int length; you should be golden.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Kevin P Smith
Ranch Hand
Joined: Feb 18, 2005
Posts: 362
|
|
|
I am now offically golden in colour! Cheers :-D
|
 |
 |
|
|
subject: Copying files corrupts file
|
|
|