Copy Paste using Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akoymakoy
    New Member
    • Oct 2006
    • 42

    #1

    Copy Paste using Java

    How do i Copy A file to another destination while overwriting the existing file in the destination folder??


    what class should i use?


    e.g.

    c:\ pic1.jpg copy to d:\pic1.jpg

    thanks in advance
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by akoymakoy
    How do i Copy A file to another destination while overwriting the existing file in the destination folder??


    what class should i use?


    e.g.

    c:\ pic1.jpg copy to d:\pic1.jpg

    thanks in advance
    What happened when you tried the advice given in this thread?

    Comment

    • vijays2
      New Member
      • Feb 2008
      • 1

      #3
      You can use below code to copy file from one folder to another

      FileChannel ic = new FileInputStream ("source file name")).getChan nel();
      FileChannel oc = new FileOutputStrea m("destinatio n file name").getChann el();
      ic.transferTo(0 , ic.size(), oc);
      ic.close();
      oc.close();

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        Originally posted by akoymakoy
        How do i Copy A file to another destination while overwriting the existing file in the destination folder??


        what class should i use?


        e.g.

        c:\ pic1.jpg copy to d:\pic1.jpg

        thanks in advance

        Another way to do that is,

        Delete the file at the destination folder if it exists,(file transfer failed if exists)

        [PHP]new File(Destinatio n File).delete();[/PHP]

        Transfer the new file from source to destination,

        [PHP]sourcefile.rena meTo(new File(Destinatio n directory, string representation of the source file));[/PHP]

        It's a filetransfering only...
        If you want to remain the sourcefile to its original directory...
        Just open it from the new destination and save it again to the original destination...

        if you implement it in automatic, then you can test it, but if in manual, use JFileChooser...

        Correct me if im wrong,
        Sukatoa, Shadow Shaman...

        Comment

        Working...