aspose file tools*
The moose likes I/O and Streams and the fly likes copy files from one folder to another in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "copy files from one folder to another in java" Watch "copy files from one folder to another in java" New topic
Author

copy files from one folder to another in java

Mai Vala
Greenhorn

Joined: Mar 31, 2010
Posts: 14
Hi All,

Can anyone help me out in writing java code to copy a list of files from one folder to another folder.

Thanks and Regards,
Mai
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Do you know how to copy a single file?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mai Vala
Greenhorn

Joined: Mar 31, 2010
Posts: 14
Rob Prime wrote:Do you know how to copy a single file?


no...I know how to move a file but I don't know how to copy a file
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Check out FileInputStream and FileOutputStream. In this FAQ entry you can see an example. Note that the ByteArrayOutputStream in this example can be replaced with any other OutputStream - such as FileOutputStream.

There are other ways to copy file, e.g. using FileChannel, but FileInputStream / FileOutputStream is the easiest to understand.
Mai Vala
Greenhorn

Joined: Mar 31, 2010
Posts: 14
Thanks....I have an idea on this but my application requires to copy some files from a folder in to another folder
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

You now know how to copy one file to another. Copying a file to a folder simply means creating a new File object based on the two:
Combine that with File.listFiles() and you should be able to handle it.
Mai Vala
Greenhorn

Joined: Mar 31, 2010
Posts: 14
java.io.FileNotFoundException: D:\Circle_Count\output\AP(this is the destination file path and a foder called AP is already created there) (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at Count.main(Count.java:64)


D:\Circle_Count\output\AP(this is the destination file path and a foder called AP is already created there)
But I am getting above exception
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

So folder is D:\Circle_Count\output\AP. That means that your destination files will be D:\Circle_Count\output\AP\xxx, with xxx being the files you need to copy.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
Mai Vala wrote:[color=red](Access is denied)


It appears you don't have rights to create files in that directory.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Not necessarily. You also get the same error when trying to write to a directory instead of a file.
Mai Vala
Greenhorn

Joined: Mar 31, 2010
Posts: 14
Rob Prime wrote:So folder is D:\Circle_Count\output\AP. That means that your destination files will be D:\Circle_Count\output\AP\xxx, with xxx being the files you need to copy.


Yes this is the case.........
Mai Vala
Greenhorn

Joined: Mar 31, 2010
Posts: 14
can anyone help me out in this..
 
 
subject: copy files from one folder to another in java