Java Random File getRandomFilename(String prefix, String sufix)

Here you can find the source of getRandomFilename(String prefix, String sufix)

Description

Returns the unique filename.

License

Open Source License

Parameter

Parameter Description
prefix can be "/tmp/tmpImage"
sufix typicaly - dot + extension ".bmp"

Return

String filename, like "/tmp/tmpImage32397371.bmp"

Declaration

public static synchronized String getRandomFilename(String prefix, String sufix) 

Method Source Code


//package com.java2s;
import java.util.*;
import java.io.*;

public class Main {
    /** Returns the unique filename.
     * @param prefix can be "/tmp/tmpImage"
     * @param sufix typicaly - dot + extension ".bmp"
     * @return String filename, like "/tmp/tmpImage32397371.bmp"
     *///from  w  ww  . ja va2 s. c om
    public static synchronized String getRandomFilename(String prefix, String sufix) {
        Random random = new Random();
        int n = random.nextInt();
        String res = prefix + Math.abs(n) + sufix;
        File file = new File(res);
        if (file.exists())
            return getRandomFilename(prefix, sufix);
        else
            return res;
    }
}

Related

  1. getRandomFile(String dir)
  2. getRandomFile(String testName)
  3. getRandomFilename(String filePath)
  4. getRandomFilenameInDirectory(File rootFolder)