Java Temp File Create getTempFilePath(String name)

Here you can find the source of getTempFilePath(String name)

Description

get Temp File Path

License

Open Source License

Parameter

Parameter Description
name prefix of the file

Return

a writable temporary file path

Declaration

public static String getTempFilePath(String name) 

Method Source Code

//package com.java2s;
/*/*from  ww  w .  j  a  v a2s  .co m*/
 * Copyright (c) 2012 Diamond Light Source Ltd.
 *
 * 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
 */

import java.io.File;

public class Main {
    /**
     * 
     * 
     * @param name prefix of the file
     * @return a writable temporary file path
     */
    public static String getTempFilePath(String name) {
        String file = "";
        String tmpfilepath = System.getProperty("java.io.tmpdir") + File.separator;
        String tmpDiamondFilePath = "/dls/tmp/";

        String username = System.getProperty("user.name");
        File tmpDir = new File(tmpfilepath);
        // if tmp directory is writable
        boolean canWrite = tmpDir.canWrite();
        if (canWrite) {
            File folderTmp = new File(tmpfilepath + username);
            if (!folderTmp.exists())
                folderTmp.mkdir();
            file = tmpfilepath + username + File.separator + "tmp_" + name;
        } else if (!canWrite) {
            File diamondFolderTmp = new File(tmpDiamondFilePath);
            if (diamondFolderTmp.canWrite()) {
                File diamondTmpUserFolder = new File(tmpDiamondFilePath + username);
                if (!diamondTmpUserFolder.exists()) {
                    diamondTmpUserFolder.mkdir();
                }
                file = tmpDiamondFilePath + username + File.separator + "tmp_" + name;
            } else {
                //TODO other case?
            }
        }
        return file;
    }
}

Related

  1. getTempFileGivenName(File file)
  2. getTempFileName()
  3. getTempFileName()
  4. getTempFileName(String base, String ext)
  5. getTempFileName(String fileExt)
  6. getTempFilePath(String signatureRequestId)
  7. getTempFiles()
  8. getTempFileWithFullPath()
  9. getUniqueAFileName(String ext)