Java mkdir makeDirs(File dir, int numTries)

Here you can find the source of makeDirs(File dir, int numTries)

Description

Create a directory and its parent directories if necessary.

License

Open Source License

Parameter

Parameter Description
dir The directory to create.
numTries The number of tries to create the directory.

Exception

Parameter Description
InterruptedException if the operation is interrupted.
IllegalArgumentException if the number of tries is less than one.

Return

true if directory creation succeeds or the directory already exists,bfalse if it fails.

Declaration

public static boolean makeDirs(File dir, int numTries) throws InterruptedException 

Method Source Code

//package com.java2s;
/*//from   w ww.  ja va 2  s  .c om
 * NOTE: This copyright does *not* cover user programs that use HQ
 * program services by normal system calls through the application
 * program interfaces provided as part of the Hyperic Plug-in Development
 * Kit or the Hyperic Client Development Kit - this is merely considered
 * normal use of the program, and does *not* fall under the heading of
 * "derived work".
 * 
 * Copyright (C) [2004, 2005, 2006], Hyperic, Inc.
 * This file is part of HQ.
 * 
 * HQ is free software; you can redistribute it and/or modify
 * it under the terms version 2 of the GNU General Public License as
 * published by the Free Software Foundation. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

import java.io.File;

public class Main {
    /**
     * Create a directory and its parent directories if necessary. If directory 
     * creation fails, it will be retried up to the specified number of tries.
     * 
     * @param dir The directory to create.
     * @param numTries The number of tries to create the directory.
     * @return <code>true</code> if directory creation succeeds or the directory
     *         already exists,b<code>false</code> if it fails.
     * @throws InterruptedException if the operation is interrupted.        
     * @throws IllegalArgumentException if the number of tries is less than one.        
     */
    public static boolean makeDirs(File dir, int numTries) throws InterruptedException {
        if (numTries < 1) {
            throw new IllegalArgumentException("number of tries must be greater than zero");
        }

        int tries = 0;
        while (tries++ < numTries) {
            boolean result = dir.mkdirs();
            if (result) {
                return true;
            } else {
                if (dir.exists()) {
                    return true;
                } else {
                    Thread.sleep(100);
                }
            }
        }
        return false;
    }
}

Related

  1. makeDirectoryWorldAccessible(File directory)
  2. makeDirForPath(String path)
  3. makeDirIfNotExists(File... paths)
  4. makeDirRecursive(File f)
  5. makeDirs(@Nullable File dir)
  6. makeDirs(File f)
  7. makeDirs(File file)
  8. makeDirs(final File file)
  9. makeDirs(String dir)