Java Temp Directory Create makeTempDirectory(String prefix, String suffix)

Here you can find the source of makeTempDirectory(String prefix, String suffix)

Description

Creates an empty directory in the default temporary-file directory, using the given prefix and suffix to generate its name.

License

Open Source License

Parameter

Parameter Description
prefix the prefix to be used in generating the directory's name; must be at least three characters long
suffix the suffix to be used in generating the directory's name; may be <code>null</code>, in which case the suffix <code>.tmp</code> will be used

Exception

Parameter Description
IOException if the directory could not be created

Return

an abstract pathname denoting a newly-created empty directory

Declaration

public static File makeTempDirectory(String prefix, String suffix) throws IOException 

Method Source Code


//package com.java2s;
/*//from  w ww.java2s.com
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the JBPM BPEL PUBLIC LICENSE AGREEMENT as
 * published by JBoss Inc.; either version 1.0 of the License, or
 * (at your option) any later version.
 *
 * This software 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.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Creates an empty directory in the default temporary-file directory, using the given prefix and
     * suffix to generate its name.
     * @param prefix the prefix to be used in generating the directory's name; must be at least three
     * characters long
     * @param suffix the suffix to be used in generating the directory's name; may be
     * <code>null</code>, in which case the suffix <code>.tmp</code> will be used
     * @return an abstract pathname denoting a newly-created empty directory
     * @throws IOException if the directory could not be created
     */
    public static File makeTempDirectory(String prefix, String suffix) throws IOException {
        File dir = File.createTempFile(prefix, suffix);

        // delete temp file
        if (!dir.delete())
            throw new IOException("could not delete temporary file: " + dir);

        // make temp directory
        if (!dir.mkdir())
            throw new IOException("could not make temporary directory: " + dir);

        return dir;
    }
}

Related

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