Java Delete Temp Directory deleteTempDir()

Here you can find the source of deleteTempDir()

Description

Delete the temporary directory.

License

Open Source License

Declaration

public static void deleteTempDir() throws IOException 

Method Source Code


//package com.java2s;
/*/* w w  w.ja v a 2  s  . co  m*/
 * Copyright (c) 2013, the Dart project authors.
 * 
 * Licensed under the Eclipse Public License v1.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.eclipse.org/legal/epl-v10.html
 * 
 * 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;
import java.io.IOException;

public class Main {
    /**
     * A temporary directory used during testing and cleared via {@link #deleteTempDir()}.
     */
    private static final File TEMP_DIR = new File(System.getProperty("java.io.tmpdir"), "AnalysisEngineTestTmp");

    /**
     * Delete the temporary directory. This should called from the {@link TestCase} tearDown method of
     * any test case which calls {@link #createTempDir(String)} or {@link #createTempFile(String)}.
     */
    public static void deleteTempDir() throws IOException {
        if (TEMP_DIR.exists()) {
            deleteDirectory(TEMP_DIR);
        }
    }

    /**
     * Delete the contents of the given directory, given that we know it is a directory.
     * 
     * @param dir the directory whose contents are to be deleted
     */
    private static void deleteDirectory(File dir) throws IOException {
        for (File file : dir.listFiles()) {
            if (file.isDirectory()) {
                deleteDirectory(file);
            } else {
                if (!file.delete()) {
                    throw new IOException("Failed to delete " + file);
                }
            }
        }
        if (!dir.delete()) {
            throw new IOException("Failed to delete " + dir);
        }
    }
}

Related

  1. deleteTempDir(File file)
  2. deleteTempDirectories(List lstTempDirectories)
  3. deleteTempDirectory(File dir)
  4. deleteTempMapset(String mapsetFolder)