Java Temp Directory Get getTempDirectory()

Here you can find the source of getTempDirectory()

Description

returns the Temp Directory of the System

License

LGPL

Return

temp directory

Declaration

public static File getTempDirectory() 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.File;
import java.io.IOException;

public class Main {
    private static File tempFile;

    /**/*from  www  .  j  av a 2  s . c  o  m*/
     * returns the Temp Directory of the System
     * @return temp directory
     */
    public static File getTempDirectory() {
        if (tempFile != null)
            return tempFile;

        String tmpStr = System.getProperty("java.io.tmpdir");
        if (tmpStr != null) {
            tempFile = new File(tmpStr);
            if (tempFile.exists()) {
                tempFile = getCanonicalFileEL(tempFile);
                return tempFile;
            }
        }
        try {
            File tmp = File.createTempFile("a", "a");
            tempFile = tmp.getParentFile();
            tempFile = getCanonicalFileEL(tempFile);
            tmp.delete();
        } catch (IOException ioe) {
        }

        return tempFile;
    }

    /**
     * Returns the canonical form of this abstract pathname.
     * @param file file to get canoncial form from it
     *
     * @return  The canonical pathname string denoting the same file or
     *          directory as this abstract pathname
     *
     * @throws  SecurityException
     *          If a required system property value cannot be accessed.
     */
    public static File getCanonicalFileEL(File file) {
        try {
            return file.getCanonicalFile();
        } catch (IOException e) {
            return file;
        }
    }
}

Related

  1. getTempDirectory()
  2. getTempDirectory()
  3. getTempDirectory()
  4. getTempDirectory()
  5. getTempDirectory()
  6. getTempDirectory()
  7. getTempDirectory(Class c)
  8. getTempDirectory(String applicationName)
  9. getTempDirectoryPath()