This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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.
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.
Joined: Nov 02, 2009
Wao thats awesome man but i need more help becauseit move the file (cut) but what if i want to copy it
Joined: Nov 02, 2009
But there is also a problem when i am doing it from JfileChooser it not work
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.