• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone pls tell me how to copy a file from one folder to another folder
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kia Phia Ben
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank Joe Ess. I think your example very good.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic