my dog learned polymorphism*
The moose likes I/O and Streams and the fly likes File copy and paste 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 copy and paste" Watch "File copy and paste" New topic
Author

File copy and paste

Vivek Moyal
Ranch Hand

Joined: Nov 02, 2009
Posts: 57
I want to make a copier so that i can copy one file or directory from one disk to another so what should i do
I m getting the path of both copy location and path location but how can i copy and paste it
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Recommended reading http://java.sun.com/docs/books/tutorial/essential/io/copy.html
Nothing UI related here. Moving to the IO forum


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

No offense Maneesh, but that link is not good yet. It's solution uses java.nio.file.Path which is not available in Java 6. It will be part of Java 7 and beyond.

The only way to do it now is to manually copy using a FileInputStream and FileOutputStream. There is a short example here, at the bottom. Just don't forget to close the streams, preferably in a finally block.

That form of copying will not copy all file attributes with it (hidden, user rights etc), and I think you'll need to call File.setLastModified() to copy the last modified date.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Rob,
Of course you are right. It was my bad. I should have been more careful before posting the link.
Thanks for correcting my snafu.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Of course your link becomes very useful automatically once Java 7 is released
Vivek Moyal
Ranch Hand

Joined: Nov 02, 2009
Posts: 57
From fileinputstream and fileoutputstream it only copy the text file or you can say that it read it and jst write it

i want to move a file from 1 place to another place
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

First you said copy, not move. Moving is easier; just call File.renameTo:
Don't be fooled by the name; renameTo will move the file if the folder of the source and destination are different. It can even move files to other partitions (at least on Windows, didn't test on Linux), and on Windows to network shares as well. As long as File can handle the path / file then File.renameTo can do so. Although file rights may prevent you, but they would prevent you to do so from Explorer as well.
Vivek Moyal
Ranch Hand

Joined: Nov 02, 2009
Posts: 57
Wao thats awesome man but i need more help becauseit move the file (cut) but what if i want to copy it
Vivek Moyal
Ranch Hand

Joined: Nov 02, 2009
Posts: 57
But there is also a problem when i am doing it from JfileChooser it not work
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You need to TellTheDetails. First you mentioned copying. Then it changed into moving. Now it is both but also from JFileChooser. No wonder we haven't been able to give you one final answer yet.

We've shown you how to copy a file through code. We've shown you how to move a file through code. No, you can't do the two in the same way (yet, with java.nio.file.Path in Java 7 it's just a different method, copyTo vs moveTo). That leaves doing it from a JFileChooser so far, unless you come up with more requirements.

Maybe Joe Ess needs to move this thread to the Swing / AWT forum since that's where we handle JFileChooser and other GUI controls. The non-GUI parts seem to be handled right now.
 
 
subject: File copy and paste
 
Similar Threads
Copying file from Java to System clipboard help
Error: Package javax.servlet not found
copying file from one folder and paste it in other folder
how to copy from MS DOS screen?
Ant Path problem