Java Temp Directory Create makeTempDirectory(String dirName, boolean deleteIfExists)

Here you can find the source of makeTempDirectory(String dirName, boolean deleteIfExists)

Description

make Temp Directory

License

Open Source License

Declaration

public static File makeTempDirectory(String dirName, boolean deleteIfExists) throws IOException 

Method Source Code


//package com.java2s;
import java.io.*;

public class Main {
    public static File makeTempDirectory(String dirName, boolean deleteIfExists) throws IOException {
        return makeTempDirectory(null, dirName, deleteIfExists);
    }//  w w w  .  j a v a  2 s.  c o  m

    public static File makeTempDirectory(File parentTempDir, String dirName, boolean deleteIfExists)
            throws IOException {
        File dir = new File(getSystemTempDirectory(parentTempDir), dirName);
        if (dir.exists()) {
            if (deleteIfExists)
                deleteDir(dir);
            else
                throw new IOException("Cannot create directory that already exists: " + dir);
        }
        dir.mkdirs();
        dir.deleteOnExit();
        return dir;
    }

    public static File getSystemTempDirectory(File parentTempDir) throws IOException {
        if (parentTempDir != null && parentTempDir.exists() && parentTempDir.isDirectory())
            return parentTempDir;

        final File t = File.createTempFile("java", ".tmp", parentTempDir);
        try {
            return t.getParentFile();
        } finally {
            t.delete();
        }
    }

    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (String child : children) {
                boolean success = deleteDir(new File(dir, child));
                if (!success) {
                    return false;
                }
            }
        }

        return dir.delete();
    }

    public static File mkdirs(String packageName, File root) {
        String[] dirs = packageName.split("\\.");

        for (String dir : dirs) {
            File f = new File(root, dir);

            if (!f.exists()) {
                f.mkdir();
            }

            root = f;
        }

        return root;
    }
}

Related

  1. makeTempDir()
  2. makeTempDir(String name)
  3. makeTempDir(String prefix)
  4. makeTempDir(String prefix)
  5. makeTempDir(String prefix, String suffix)
  6. makeTempDirectory(String prefix, String suffix)
  7. makeTemporaryDirectory()
  8. makeTemporaryDirectory(File baseDir, String prefix)
  9. makeTemporaryDirectory(String dir)