JDK-6248451 : File.renameTo(File) should have option to overwrite existing destination directory
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2005-03-31
  • Updated: 2010-04-02
  • Resolved: 2009-02-16
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux prtgrp-linux 2.4.18-14 #1 Wed Sep 4 13:35:50 EDT 2002 i686 i686 i386 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
I find 2 issues:
1) I want to move a directory say "src" to "bkp".If "bkp" already exists, i want to overwrite that directory.For this, I cannot do it using File's renameTo(), since it does not write "src" directory to "bkp", if "bkp" already exists.

2) I want to do the following linux cmd "rm -rf bkp".There is no option in delete() to delete the contents for the directory recursively.

code snippet below for (1):
path = "/home/uprakash/src";
System.out.println ("uninstall - path = "+ path);
File    sourceDir = new File(path);

        if (sourceDir.exists() != true) {
                System.out.println("src does not exists!!!!!!!");
        System.exit(1);
        }

            String  destDirPath = sourceDir.getParent() + File.separator  + "bkp";
            boolean success = false;
            File    destDir = new File (destDirPath);
                System.out.println ("path = "+ destDirPath);

  boolean success = false;

            /* Check if dest dir exists.If not, create.
            */
            if (destDir.exists() != true) {
                success = destDir.mkdir();
                if (!success) {
                    System.err.println ("Dir creation failed !");
                    return false;
                }
            } else {
            System.out.println("DIR exists !!!!!!!");
            }

            /* Move file to new destDirectory
            */
  success = sourceDir.renameTo(new File(destDir, sourceDir.getName()));
            //success = sourceDir.renameTo(destDir);
            if (!success) {
                System.err.println ("Dir Not successfully moved!");
                return false;
            }
} catch (Exception e) {
    e.printStackTrace();
}



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Pls refer the code snippet in desc.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Option in renameTo to overwrite an existing directory.
ACTUAL -
Does not overwrite an existing directory.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
same code snippet:

path = "/home/uprakash/src";
System.out.println ("uninstall - path = "+ path);
File    sourceDir = new File(path);

        if (sourceDir.exists() != true) {
                System.out.println("src does not exists!!!!!!!");
        System.exit(1);
        }

            String  destDirPath = sourceDir.getParent() + File.separator  + "bkp";
            boolean success = false;
            File    destDir = new File (destDirPath);
                System.out.println ("path = "+ destDirPath);

  boolean success = false;

            /* Check if dest dir exists.If not, create.
            */
            if (destDir.exists() != true) {
                success = destDir.mkdir();
                if (!success) {
                    System.err.println ("Dir creation failed !");
                    return false;
                }
            } else {
            System.out.println("DIR exists !!!!!!!");
            }

            /* Move file to new destDirectory
            */
  success = sourceDir.renameTo(new File(destDir, sourceDir.getName()));
            //success = sourceDir.renameTo(destDir);
            if (!success) {
                System.err.println ("Dir Not successfully moved!");
                return false;
            }
} catch (Exception e) {
    e.printStackTrace();
}
---------- END SOURCE ----------
###@###.### 2005-03-31 01:40:05 GMT

Comments
EVALUATION This feature has been addressed by the new file system API defined by JSR-203.
16-02-2009

EVALUATION No OS command like mv or cp would recursively delete the target directory name. That would be much too dangerous. I don't think the java API would ever provide that. On the other hand.... something like "rm -rf" is extremely difficult (impossible?) to implement in Java, especially if you want a robust implementation in the face of a clever and determined adversary. ###@###.### 2005-04-19 01:36:36 GMT
19-04-2005