File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Copying files corrupts file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Copying files corrupts file" Watch "Copying files corrupts file" New topic
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
    
  15

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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Copying files corrupts file
 
Similar Threads
Out of Memory exception
Download large files in RMI
Send a file to server
Please help with response.getoutputStream
Writing non text file