my dog learned polymorphism*
The moose likes I/O and Streams and the fly likes how to copy a file from one folder to another folder? 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 "how to copy a file from one folder to another folder?" Watch "how to copy a file from one folder to another folder?" New topic
Author

how to copy a file from one folder to another folder?

senthiltmkumar
Greenhorn

Joined: Mar 01, 2001
Posts: 5
Can anyone pls tell me how to copy a file from one folder to another folder
Kia Phia Ben
Ranch Hand

Joined: Dec 16, 2005
Posts: 48
Hi senthilkumar
I hope I help you:
copy file:

File file = new File("path of source file");
if(file.exists){
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream("path of new file");
byte[] buff = new byte[fis.available()];
fis.read(buff);
fos.write(buff);
}
senthiltmkumar
Greenhorn

Joined: Mar 01, 2001
Posts: 5
Thanks for ur reply.ur code is correct for one text file.
I have a folder which contains lot of text files and image files.
I want to copy all files from one folder to another.
Can any one pls help me?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

senthikumar, you have already been asked once to change your display name to conform to the JavaRanch Naming Policy. I must warn you that if you do not comply, your account will be closed.
You can change your name here.
As for Kia's code, it has one fatal error: available() doesn't do what you think it does.
Have a look at this example for a copy method that works.
To copy an entire directory, use the methods of java.io.File to get a list of the directory contents and copy each file individually.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Kia Phia Ben
Ranch Hand

Joined: Dec 16, 2005
Posts: 48
Thank Joe Ess. I think your example very good.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to copy a file from one folder to another folder?
 
Similar Threads
Need script to copy file on FTP server from Windows
How to move files in struts
copy netbeans project fiel
how to copy a .doc file through java
copy paste image