Java Utililty Methods Random File

List of utility methods to do Random File

Description

The list of methods to do Random File are organized into topic(s).

Method

StringgetRandomFile(String dir)
Get a random file from a directory.
File directory = new File(dir);
int count = 0;
String randomFileName = "";
if (directory.exists()) {
    File[] listFiles = directory.listFiles();
    int numFiles = listFiles.length;
    Random rand = new Random(System.currentTimeMillis());
    int index = rand.nextInt(numFiles - 1);
...
StringgetRandomFile(String testName)
get Random File
File parent = getTestDir();
File result = new File(parent, getRandomFileName(testName));
return result.getCanonicalPath();
StringgetRandomFilename(String filePath)
get Random Filename
return getRandomFilename(filePath, "tmp");
StringgetRandomFilename(String prefix, String sufix)
Returns the unique filename.
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;
...
FilegetRandomFilenameInDirectory(File rootFolder)
get Random Filename In Directory
String fileName = "rndFile-" + System.currentTimeMillis() + "-" + Math.abs(randomGen.nextInt()) + ".dat";
File newRandomFile = new File(rootFolder, fileName);
return newRandomFile;