Java Temp File Create getTempFileName(String base, String ext)

Here you can find the source of getTempFileName(String base, String ext)

Description

Get a temporary file name, optionally prefixed by the base (if specified) and also optionally using the given extension (if specified, otherwise ".tmp" will be used).

License

Open Source License

Parameter

Parameter Description
base The prefix for the file name to use (if not specified, none is used).
ext File extension to use, if not specified ".tmp" will be used.

Return

The temporary file name to use.

Declaration

public static File getTempFileName(String base, String ext) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Firestar Software, Inc.
 * 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://from  ww  w  .j  av a 2s. co  m
 *     Firestar Software, Inc. - initial API and implementation
 *
 * Author:
 *     Gabriel Oancea
 *
 *******************************************************************************/

import java.io.*;
import java.util.*;

public class Main {
    private static final String INVALID_FILE_NAME_CHARS = "\\|/:*?\"<>,'`; ";

    /**
     * Get a temporary file name, optionally prefixed by the base (if specified) and also optionally using the given
     * extension (if specified, otherwise ".tmp" will be used). The file folder will be the one specified in the
     * "java.io.tmpdir" environment variable. The file name will look something like:
     * <p>
     * <code>baseRANDOM_UUID.tmp</code>
     * 
     * @param base The prefix for the file name to use (if not specified, none is used).
     * @param ext File extension to use, if not specified ".tmp" will be used.
     * @return The temporary file name to use.
     */
    public static File getTempFileName(String base, String ext) {
        if (base == null || base.length() <= 0)
            base = "";
        if (ext == null || ext.length() <= 0)
            ext = "tmp";
        UUID uuid = UUID.randomUUID();
        String tmpName = sanitizeFileName(base + uuid.toString() + '.'
                + ext);
        return new File(System.getProperty("java.io.tmpdir"), tmpName);
    }

    /**
     * Return a valid file name, replacing all invalid characters with '_'.
     * 
     * @param s Incoming string to be sanitized.
     * @return A valid file name.
     */
    public static String sanitizeFileName(String s) {
        if (s == null || s.length() <= 0)
            return s;
        StringBuffer sb = new StringBuffer(s);
        for (int i = 0; i < sb.length(); i++) {
            char c = sb.charAt(i);
            if (INVALID_FILE_NAME_CHARS.indexOf(c) >= 0)
                sb.setCharAt(i, '_');
        }
        return sb.toString();
    }
}

Related

  1. getTempFileDir()
  2. getTempFileFor(File file)
  3. getTempFileGivenName(File file)
  4. getTempFileName()
  5. getTempFileName()
  6. getTempFileName(String fileExt)
  7. getTempFilePath(String name)
  8. getTempFilePath(String signatureRequestId)
  9. getTempFiles()