General discussion

  • Creator
    Topic
  • #2107033

    moving files in java

    Locked

    by ashapoo_jain ·

    I want to move files from one folder to another in Java. I used the renameTo method in the File class but that doesnt helps me…
    Am posting my code here, can some one look at it and tell me whats wring in it and also is there an efficient way to move files in Java across folders…

    public boolean moveFiles(String source, String destination)
    {
    //This method moves the files from one directory to another
    File sourceDir = new File(source.trim());
    File destinationDir = new File(destination.trim());

    boolean created = true;
    if(!destinationDir.exists())
    {
    //Create that directory

    created = destinationDir.mkdir();
    System.out.println(“The directory created ” + created);

    //do the moving of files here

    File [] tmp = sourceDir.listFiles();
    for(int i =0;i

All Comments

  • Author
    Replies
    • #3818572

      moving files in java

      by sgovindu ·

      In reply to moving files in java

      renameTo seems not to work with driectories. In your loop of renameTo try the following:

      for(int i = 0; i < tmp.length; i++) { File newFile = new File(destinationDir, tmp[i].getName()); tmp[i].renameTo(newFile); }

    • #3691566

      moving files in java

      by ashapoo_jain ·

      In reply to moving files in java

      This question was closed by the author

Viewing 1 reply thread