Java Temp Directory Get getTempDirectory()

Here you can find the source of getTempDirectory()

Description

Return the directory where temporary files are created.

License

Open Source License

Exception

Parameter Description
IOException an exception

Return

the directory where temporary files are created.

Declaration

public static String getTempDirectory() throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 United States Government as represented by the
 * Administrator of the National Aeronautics and Space Administration.
 * All Rights Reserved./*from   www  .  j ava2s. c  o  m*/
 * 
 * Licensed 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.File;

import java.io.IOException;

public class Main {
    /**
     * Return the directory where temporary files are created.
     * 
     * @return the directory where temporary files are created.
     * @throws IOException
     */
    public static String getTempDirectory() throws IOException {
        String tempDirectory = System.getProperty("java.io.tmpdir"); // works in JDK 1.4+
        if (tempDirectory != null) {

            return tempDirectory;
        }
        File f = File.createTempFile("directory-finder", ".tmp"); // fall back for politeness
        f.delete();
        return f.getParent();
    }

    /**
     * Create a temporary file that will auto-delete when this VM terminates
     * 
     * @param prefix
     * @param suffix
     * @return File
     * @throws IOException
     */
    public static File createTempFile(String prefix, String suffix) throws IOException {
        File tempFile = File.createTempFile(prefix, suffix);
        tempFile.deleteOnExit();
        return tempFile;
    }

    /**
     * Create a temporary file that will auto-delete when this VM terminates
     * 
     * @param prefix
     * @param suffix
     * @param directory
     * @return File
     * @throws IOException
     */
    public static File createTempFile(String prefix, String suffix, File directory) throws IOException {
        File tempFile = File.createTempFile(prefix, suffix, directory);
        tempFile.deleteOnExit();
        return tempFile;
    }
}

Related

  1. getTempDir(String prefix)
  2. getTempDir(String suffix)
  3. getTempDirectory()
  4. getTempDirectory()
  5. getTempDirectory()
  6. getTempDirectory()
  7. getTempDirectory()
  8. getTempDirectory()
  9. getTempDirectory()