Java Temp Folder Create getTempPath()

Here you can find the source of getTempPath()

Description

get Temp Path

License

Open Source License

Declaration

public static String getTempPath() 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 www.isandlatech.com (www.isandlatech.com)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*  w  ww  . j  a  v  a 2 s .c o  m*/
 *    ogattaz (isandlaTech) - initial API and implementation
 *******************************************************************************/

import java.io.File;

public class Main {
    private final static String FILE_EXT_TXT = "txt";
    private final static String FILE_NAME_DUMMY = "dummy";
    public static final String SYS_PROPERTY_TMPDIR = "java.io.tmpdir";
    public static final String SYS_PROPERTY_USERDIR = "user.dir";

    /**
     * @return
     */
    public static String getTempPath() {

        String wPath = System.getProperty(SYS_PROPERTY_TMPDIR);

        if (wPath == null || wPath.length() == 0) {
            // System.out.println(
            // "Temporary directory is unknown ('java.io.tmpdir'), using directory used by 'File.createTempFile'."
            // );

            wPath = getTempAbsolutePath();

            if (wPath == null || wPath.length() == 0) {
                // System.out.println("'File.createTempFile' doesn't return directory, using current directory ('user.dir').");

                wPath = System.getProperty(SYS_PROPERTY_USERDIR);
            }
        }
        return wPath;
    }

    /**
     * @return
     */
    private static String getTempAbsolutePath() {

        String wPath = null;
        try {
            File wTempFile = File.createTempFile(FILE_NAME_DUMMY,
                    FILE_EXT_TXT);
            wPath = wTempFile.getParent();
            wTempFile.delete();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return wPath;
    }
}

Related

  1. getTempPath()
  2. getTempPath()
  3. getTempPath()
  4. GetTempPath()
  5. getTempPath()
  6. getTempPath()
  7. getTempPath()