• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

copy files from one folder to another in java

 
Mai Vala
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to copy a single file?
 
Mai Vala
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mai Vala wrote:[color=red](Access is denied)


It appears you don't have rights to create files in that directory.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not necessarily. You also get the same error when trying to write to a directory instead of a file.
 
Mai Vala
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone help me out in this..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic