Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6248451
Votes 0
Synopsis File.renameTo(File) should have option to overwrite existing destination directory
Category java:classes_io
Reported Against
Release Fixed
State 11-Closed, duplicate of 4313887, request for enhancement
Priority: 4-Low
Related Bugs
Submit Date 31-MAR-2005
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 ----------
  xxxxx@xxxxx   2005-03-31 01:40:05 GMT
Work Around
N/A
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.
  xxxxx@xxxxx   2005-04-19 01:36:36 GMT
This feature has been addressed by the new file system API defined by JSR-203.
Posted Date : 2009-02-16 15:48:32.0
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang