Java Create Directory mkdirsWithRetry(final File f)

Here you can find the source of mkdirsWithRetry(final File f)

Description

A thread-safer mkdirs.

License

Open Source License

Declaration

public static boolean mkdirsWithRetry(final File f) 

Method Source Code

//package com.java2s;
/**/*from   ww  w  .jav  a 2  s  . c om*/
 * Copyright 2015 Palantir Technologies
 *
 * Licensed under the BSD-3 License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

public class Main {
    /**
     * A thread-safer mkdirs. mkdirs() will fail if another thread creates one of the directories it
     * means to create before it does. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4742723
     *
     * This attempts the mkdirs() multiple times, so that if another thread does create one of the
     * directories, the next call to mkdirs() will take that into account.
     *
     * Note: this returns true iff the file f ends up being a directory, contrary to mkdirs(), which
     * returns false if it existed before the call.
     */
    public static boolean mkdirsWithRetry(final File f) {
        if (f.exists()) {
            return f.isDirectory();
        }

        for (int i = 0; i < 10 && !f.isDirectory(); ++i) {
            f.mkdirs();
        }

        return f.isDirectory();
    }
}

Related

  1. mkdirsifnotExists(String s)
  2. mkdirsInRemote(String host, String path, boolean deleteIfExisted)
  3. mkdirsOrCrash(File d)
  4. mkdirsOrFail(File dirFile)
  5. mkdirsWithExistsCheck(File dir)