Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File copy and paste

 
Vivek Moyal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recommended reading http://java.sun.com/docs/books/tutorial/essential/io/copy.html
Nothing UI related here. Moving to the IO forum
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course your link becomes very useful automatically once Java 7 is released
 
Vivek Moyal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But there is also a problem when i am doing it from JfileChooser it not work
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic