Java Temp Directory Create makeTempDir(String prefix)

Here you can find the source of makeTempDir(String prefix)

Description

make Temp Dir

License

Open Source License

Return

File temporary directory with the given prefix

Declaration

public static File makeTempDir(String prefix) 

Method Source Code


//package com.java2s;
/* *******************************************************************
 * Copyright (c) 1999-2001 Xerox Corporation, 
 *               2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved. //from   ww w  .  ja  va 2 s.  co m
 * This program and the accompanying materials are made available 
 * under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors: 
 *     Xerox/PARC     initial implementation 
 * ******************************************************************/

import java.io.*;

public class Main {
    /** @return File temporary directory with the given prefix */
    public static File makeTempDir(String prefix) {
        if (null == prefix) {
            prefix = "tempDir";
        }
        File tempFile = null;
        for (int i = 0; i < 10; i++) {
            try {
                tempFile = File.createTempFile(prefix, "tmp");
                tempFile.delete();
                if (tempFile.mkdirs()) {
                    break;
                }
                tempFile = null;
            } catch (IOException e) {
            }
        }
        return tempFile;
    }

    /** 
     * Delete file or directory.
     * @param dir the File file or directory to delete.
     * @return true if all contents of dir were deleted 
     */
    public static boolean delete(File dir) {
        return deleteContents(dir) && dir.delete();
    }

    /** 
     * Delete contents of directory.
     * The directory itself is not deleted.
     * @param dir the File directory whose contents should be deleted.
     * @return true if all contents of dir were deleted 
     */
    public static boolean deleteContents(File dir) {
        if ((null == dir) || !dir.canWrite()) {
            return false;
        } else if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (!deleteContents(files[i]) || !files[i].delete()) {
                    return false;
                }
            }
        }
        return true;
    }
}

Related

  1. createTempDirectory(String prefix)
  2. createTempDirectory(String prefix)
  3. makeTempDir()
  4. makeTempDir(String name)
  5. makeTempDir(String prefix)
  6. makeTempDir(String prefix, String suffix)
  7. makeTempDirectory(String dirName, boolean deleteIfExists)
  8. makeTempDirectory(String prefix, String suffix)
  9. makeTemporaryDirectory()