Java Temp File Create getUniqueFileName(String originalFileName)

Here you can find the source of getUniqueFileName(String originalFileName)

Description

Gets a unique file name, based on the fileName with the time in milliseconds added before the file extension., This works with all file types including directories.

License

Open Source License

Parameter

Parameter Description
originalFileName a parameter

Return

a unique file name based on current time.

Declaration

static public String getUniqueFileName(String originalFileName) 

Method Source Code


//package com.java2s;
/*//from  w ww .jav a2  s . com
 * Copyright 2012 University of Denver
 * Author Chet Rebman
 * 
 * This file is part of FedoraApp.
 * 
 * FedoraApp 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.
 * 
 * FedoraApp 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 FedoraApp.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;

import java.text.SimpleDateFormat;

public class Main {
    static public String fileDataStringMilliSecondFormat = "MMMMM-dd-yyyy_HH:mm:ss-SS";
    static protected int MAX_TRIES = 100000;
    static String lastFileName = "";

    /**
     * Gets a unique file name, based on the fileName with the time in milliseconds added before the file extension., 
     * This works with all file types including directories.
     * 
     * @return a unique file name based on current time.
     * @param originalFileName  
     */
    static public String getUniqueFileName(String originalFileName) {

        int index = originalFileName.lastIndexOf('.');

        if (index < 0)
            index = originalFileName.length();

        String suffix = originalFileName.substring(index);

        originalFileName = originalFileName.replace(suffix, "");

        String newFileName = originalFileName + "_" + getDateTimeMilliSecondEnsureUnique() + suffix;

        return newFileName;
    }

    /**     
     * Return a string containing the current date and time to the second of type april-30-2008:093301 that can be used 
     * for a file name. If the file already exists keep trying until the file name is unique up to MAX_TRIES. This routine 
     * therefore can only return one file name per java millisecond at best.
     * <br>
     * @see #MAX_TRIES
     * @return String with current/unique date and time to the millisecond
     * @throws RuntimeException
     */
    static public String getDateTimeMilliSecondEnsureUnique() {

        String fileName = getDateTimeMilliSecondFileName();
        boolean newFileCreated = false;
        int count = 0;

        while (!newFileCreated) {
            fileName = getDateTimeMilliSecondFileName();
            newFileCreated = !fileName.equals(lastFileName);
            count++;
            if (count >= MAX_TRIES) {
                throw new RuntimeException("Unable to get unique file :" + fileName);
            }
        }

        lastFileName = fileName;

        return fileName;
    }

    /**
     * Return a string containing the current date and time to the second of type april-30-2008:093301 that can be used 
     * for a file name. 
     * <br>
     * <br>
     * NOTE: ONLY ONE UNIQUE FILE NAME PER MILLISECOND!
     * 
     * @see #getDateTimeMilliSecondEnsureUnique()
     * @return a string of type april-30-2008:0933
     */
    static public String getDateTimeMilliSecondFileName() {

        Date now = new Date(Calendar.getInstance().getTimeInMillis());

        SimpleDateFormat dateFormat = new SimpleDateFormat(fileDataStringMilliSecondFormat);

        String fileName = dateFormat.format(now).toLowerCase();

        return fileName;
    }
}

Related

  1. getUniqueAFileName(String ext)
  2. getUniqueFile(File dir, String prefix, String suffix)
  3. getUniqueFileName()
  4. getUniqueFilename(String filename, String extension)
  5. getUniqueFileName(String fileNamePrefix, String fileExtension)
  6. getUniqueTempFile(final boolean autoDelete, final File parentFolder, final String suffix)
  7. newTmpFile(String content)
  8. newTmpFile(String content, String encoding)