5 Replies Latest reply on Aug 29, 2011 8:27 PM by 693129

    File.mkdirs intermittently failing on Windows 7 64 bit

    843790
      I have a workstation with Windows 7 64 bit on it that has two hard drives. The OS is on c: and d: is for my development and source code. When I run the following simple test, baseDir.mkdirs() intermittently fails (once every three or four runs) but only when I create the baseDir on the d: drive and only on the first run through the loop if the javatests directory exists from a prior run. If I create the baseDir directory on the c: drive or use
      Windows Explorer to delete the javatests directory before running the test, it seems to always succeed. The sub1.mkdirs always seems to succeed as well so it is almost as if something is cached and then gets cleared before it reaches that statement.

      Has anyone seen any similar behavior and can offer any help? There are no errors or warnings in the Windows Event logs.
      import java.io.File;
      import java.io.FileWriter;
      
      public class DirTest {
           public static void main(String [] args) throws Exception {
                File baseDir = new File(System.getProperty("test.dir", "d:/javatests"));
                for (int i = 0; i < 10; i++) {
                     deleteDirs(baseDir);
      
                     if (!baseDir.mkdirs()) {
                          System.err.println("Failed " + i);
                     }
      
                     File sub1 = new File(baseDir, "test");
                     if (!sub1.mkdirs())  {
                          System.err.println("Failed sub1");
                     }
      
                     File sub2 = new File(baseDir, "test2");
                     if (!sub2.mkdirs()) {
                          System.err.println("Failed sub2");
                     }
      
                     File sec1 = new File(baseDir, "default.sec");
                     File sec2 = new File(sub1, "test.sec");
      
                     FileWriter fw = new FileWriter(sec1);
                     fw.write("<security>\n " +
                                "<ownerpassword>default</ownerpassword>\n " +
                                "<userpassword strength=\"40\" allowprinting=\"FALSE\">default</userpassword>" +
                                "</security>");
                     fw.close();
      
                     fw = new FileWriter(sec2);
                     fw.write("<security>\n " +
                                "<ownerpassword>test</ownerpassword>\n " +
                                "<userpassword strength=\"128\" allowprinting=\"FALSE\" allowcopy=\"false\">test</userpassword> "+
                                "</security>");
      
                     fw.close();
      
                }
      
           }
      
           private static void deleteDirs(File f) {
                if (f.exists()) {
                     String [] children = f.list();
                     if (children != null)
                          for (int x = 0; x < children.length; x++) {
                               File child = new File(f, children[x]);
                               if (child.isDirectory())
                                    deleteDirs(child);
                               else
                                    if (!child.delete())
                                         System.err.println("Failed to delete child " + child.getName());
                          }
                     if (!f.delete()) {
                          System.err.println("Failed to delete " + f.getName());
                     }
                }
           }
      
      }