Java Temp Directory Get getTempDirectoryStr()

Here you can find the source of getTempDirectoryStr()

Description

Returns the temporary directory as string.

License

Open Source License

Return

the temp directory

Declaration

public static synchronized String getTempDirectoryStr() 

Method Source Code


//package com.java2s;
/*//from ww  w  .j  a  v  a 2  s . c om
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

public class Main {
    /** property indicating the temp directory to use. */
    public final static String PROPERTY_TMPDIR = "adams.io.tmpdir";
    /** the temporary directory. */
    protected static String m_TempDir;

    /**
     * Returns the temporary directory as string.
     *
     * @return   the temp directory
     */
    public static synchronized String getTempDirectoryStr() {
        String dir;
        File file;

        if (m_TempDir == null) {
            dir = System.getProperty(PROPERTY_TMPDIR);
            if (dir == null) {
                dir = System.getProperty("java.io.tmpdir");
            } else {
                file = new File(dir);
                if (!file.exists()) {
                    if (!file.mkdirs()) {
                        dir = System.getProperty("java.io.tmpdir");
                        System.err.println("Failed to create temp directory '" + file
                                + "', reverting back to system's default: " + dir);
                    }
                }
            }
            try {
                m_TempDir = new File(dir).getCanonicalPath();
            } catch (Exception e) {
                m_TempDir = dir;
            }
        }

        return m_TempDir;
    }
}

Related

  1. getTempDirectory()
  2. getTempDirectory(Class c)
  3. getTempDirectory(String applicationName)
  4. getTempDirectoryPath()
  5. getTempDirectoryPathString()
  6. getTempDirFile()
  7. getTempDirFilePath(String name)
  8. getTempDirName()
  9. getTempDirURL()