Here you can find the source of createTempDir(String suffix)
public static File createTempDir(String suffix) throws IOException
//package com.java2s; /*/* ww w. j av a 2s . c o m*/ * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * 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.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.UUID; public class Main { private static final File TMP = new File(System.getProperty("java.io.tmpdir"), "aether-" + UUID.randomUUID().toString().substring(0, 8)); public static File createTempDir() throws IOException { return createTempDir(""); } public static File createTempDir(String suffix) throws IOException { mkdirs(TMP); File tmpFile = File.createTempFile("tmpdir-", suffix, TMP); deleteFile(tmpFile); mkdirs(tmpFile); return tmpFile; } public static boolean mkdirs(File directory) { if (directory == null) { return false; } if (directory.exists()) { return false; } if (directory.mkdir()) { return true; } File canonDir = null; try { canonDir = directory.getCanonicalFile(); } catch (IOException e) { return false; } File parentDir = canonDir.getParentFile(); return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir()); } public static File createTempFile(String contents) throws IOException { return createTempFile(contents.getBytes(StandardCharsets.UTF_8), 1); } public static File createTempFile(byte[] pattern, int repeat) throws IOException { mkdirs(TMP); File tmpFile = File.createTempFile("tmpfile-", ".data", TMP); writeBytes(tmpFile, pattern, repeat); return tmpFile; } public static void deleteFile(File file) throws IOException { if (file == null) { return; } Collection<File> undeletables = new ArrayList<File>(); delete(file, undeletables); if (!undeletables.isEmpty()) { throw new IOException("Failed to delete " + undeletables); } } public static void writeBytes(File file, byte[] pattern, int repeat) throws IOException { file.deleteOnExit(); file.getParentFile().mkdirs(); OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(file)); for (int i = 0; i < repeat; i++) { out.write(pattern); } out.close(); out = null; } finally { try { if (out != null) { out.close(); } } catch (final IOException e) { // Suppressed due to an exception already thrown in the try block. } } } private static void delete(File file, Collection<File> undeletables) { String[] children = file.list(); if (children != null) { for (String child : children) { delete(new File(file, child), undeletables); } } if (!del(file)) { undeletables.add(file.getAbsoluteFile()); } } private static boolean del(File file) { for (int i = 0; i < 10; i++) { if (file.delete() || !file.exists()) { return true; } } return false; } }