Java Random File getRandomFilename(String filePath)

Here you can find the source of getRandomFilename(String filePath)

Description

get Random Filename

License

Open Source License

Declaration

public static String getRandomFilename(String filePath) 

Method Source Code

//package com.java2s;
/**/*ww w.  j ava2s  . c o  m*/
 *  
 * Copyright (c) 2015 Fannie Mae, All rights reserved.
 * This program and the accompany materials are made available under
 * the terms of the Fannie Mae Open Source Licensing Project available 
 * at https://github.com/FannieMaeOpenSource/ezPie/wiki/License
 * 
 * ezPIE? is a registered trademark of Fannie Mae
 * 
 */

import java.io.File;

import java.util.UUID;

public class Main {
    public static String getRandomFilename(String filePath) {
        return getRandomFilename(filePath, "tmp");
    }

    public static String getRandomFilename(String filePath, String extension) {
        String randomGuid = UUID.randomUUID().toString().replace("-", "");
        String directory = filePath;
        if (!directory.endsWith(File.separator)) {
            directory += File.separator;
        }
        if ((extension != null) && extension.startsWith(".") && (extension.length() > 1)) {
            extension = extension.substring(1);
        }
        return String.format("%s%s.%s", directory, randomGuid, extension);
    }
}

Related

  1. getRandomFile(String dir)
  2. getRandomFile(String testName)
  3. getRandomFilename(String prefix, String sufix)
  4. getRandomFilenameInDirectory(File rootFolder)