Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6248451

File.renameTo(File) should have option to overwrite existing destination directory

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.2
    • core-libs
    • x86
    • linux

      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

            alanb Alan Bateman
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: