jQuery in Action, 2nd edition*
The moose likes I/O and Streams and the fly likes File copying 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 "File copying " Watch "File copying " New topic
Author

File copying

Nijin puthiya purayil
Ranch Hand

Joined: Nov 09, 2011
Posts: 32
Hai,
I have two files in my temp directory...i want to copy these two files to another file in the same directory...is it possible by using FileInputStream() and FileOutputStream()??? I'm confused in using the for loop...
where can i use the for loop??Below is my code snippet
Please do help me...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Your copying code itself is correct with one exception - 0 is a valid return valid for read; you should check for >= 0, > -1 or != -1 instead of > 0.
Now all you need to do is call that code twice. That means that you should put the copying code in a separate method; in fact, several libraries have utility methods for that.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nijin puthiya purayil
Ranch Hand

Joined: Nov 09, 2011
Posts: 32
Thanks Rob,
But now i can find only one file in my destination file...i did as you said...i replaced >0 to != -1 and placed the copying method separately...
can there be any problem with the extension of the file??here the extension for both source and destination files are .deb....
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

How are you calling the copy code now, for both files?
Nijin puthiya purayil
Ranch Hand

Joined: Nov 09, 2011
Posts: 32
i'm not passing any arguments here...do i have to pass??
i'm calling the method after the OutputStream os = new FileOutputStream(file2,true);...and placed the while loop inside that method...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: File copying
 
Similar Threads
Adding files to existing zip archive
unable to traverse directory recursevely and to create soft-link !!!
copy to app server
SequenceInputStream performance
Problem in setBinaryStream when using SequenceInputStream