Java Temp Directory Get getTempDirectory(String applicationName)

Here you can find the source of getTempDirectory(String applicationName)

Description

Returns a temporary directory for a given application

License

Open Source License

Parameter

Parameter Description
applicationName The name of the application

Return

The requested path.

Declaration

public static String getTempDirectory(String applicationName) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    /**/* w w  w.  j a v  a 2  s .c o m*/
     * Returns a temporary directory for a given application
     * 
     * @param applicationName
     *            The name of the application
     * @return The requested path.
     */
    public static String getTempDirectory(String applicationName) {
        String tempDir = getApplicationDataDirectory(applicationName) + "/temp/";
        File tempDirFile = new File(tempDir);

        if (!tempDirFile.exists()) {
            // Create it
            tempDirFile.mkdirs();
        }
        return tempDirFile.getAbsolutePath() + File.separatorChar;
    }

    /**
     * This method finds the application data directory and creates (if it
     * doesn't exist) the directory corresponding to the application. (based on
     * the applicationName parameter) Under Linux, a directory is created in the
     * home of the user named .applicationName under windows, the directory is
     * creates in %APPDATA%/Intel/applicationName.
     * 
     * @param applicationName
     *            The name of the application
     * @return The string representing the created directory.
     */
    public static String getApplicationDataDirectory(String applicationName) {
        String os = getOsName();
        String appDataPath = getApplicationDataDirectory();
        String fileSeparator = System.getProperty("file.separator");

        if (os.contains("Linux")) {
            appDataPath += fileSeparator + "." + applicationName;
        } else /* Windows */
        {
            appDataPath += fileSeparator + "Intel" + fileSeparator + applicationName;
        }

        File appDataFile = new File(appDataPath);

        if (!appDataFile.exists()) {
            // Create it
            appDataFile.mkdirs();
        }

        return (appDataFile.getPath() + File.separatorChar);

    }

    /**
     * Get the application data directory.
     * 
     * @return The string representing the application data directory.
     */
    public static String getApplicationDataDirectory() {
        String os = getOsName();
        String appDataPath;

        if (os.contains("Linux")) {
            appDataPath = System.getProperty("user.home");
        } else /* Windows */
        {
            appDataPath = System.getenv("APPDATA");
        }

        return (appDataPath + File.separatorChar);
    }

    /**
     * The name of the running OS
     * 
     * @return
     */
    public static String getOsName() {
        return System.getProperty("os.name");
    }
}

Related

  1. getTempDirectory()
  2. getTempDirectory()
  3. getTempDirectory()
  4. getTempDirectory()
  5. getTempDirectory(Class c)
  6. getTempDirectoryPath()
  7. getTempDirectoryPathString()
  8. getTempDirectoryStr()
  9. getTempDirFile()