Skip to content
Snippets Groups Projects
Commit f44dced9 authored by ribeaudc's avatar ribeaudc
Browse files

[LMS-400]

add: - 'FileUtilities.createTempFile' (tested).

SVN: 5828
parent 7d1e1b2e
No related branches found
No related tags found
No related merge requests found
......@@ -42,6 +42,7 @@ import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import ch.systemsx.cisd.common.exceptions.CheckedExceptionTunnel;
......@@ -1054,4 +1055,35 @@ public final class FileUtilities
}
}
/**
* Create a temporary file in a given directory.
* <p>
* The file denoted by the returned abstract pathname did not exist before this method was
* invoked, any subsequent invocation of this method will yield a different file name.
* </p>
* <p>
* The filename is prefixNNNNsuffix where NNNN is a random number.
* </p>
* <p>
* This method is different from File.createTempFile() of JDK 1.2 as it doesn't create the file
* itself. It uses the location pointed to by java.io.tmpdir when the parentDir attribute is
* null.
* </p>
*/
public final static File createTempFile(final String prefixOrNull, final String suffixOrNull,
final File parentDirOrNull)
{
File result = null;
final String parent =
(parentDirOrNull == null) ? System.getProperty("java.io.tmpdir") : parentDirOrNull
.getPath();
do
{
result =
new File(parent, StringUtils.defaultString(prefixOrNull)
+ RandomStringUtils.randomNumeric(4)
+ StringUtils.defaultString(suffixOrNull));
} while (result.exists());
return result;
}
}
......@@ -36,6 +36,7 @@ import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.testng.annotations.Test;
import ch.systemsx.cisd.common.Constants;
......@@ -500,4 +501,27 @@ public final class FileUtilitiesTest extends AbstractFileSystemTestCase
assertEquals(canonicalPath, FileUtilities.normalizeFile(file).getAbsolutePath());
}
@Test
public final void testCreateTempFile()
{
File file = FileUtilities.createTempFile(null, null, null);
assertNotNull(file);
assertFalse(file.exists());
String name = file.getName();
assertTrue(name.length() == 4);
NumberUtils.isNumber(name);
String prefix = System.getProperty("java.io.tmpdir");
assertTrue(file.getAbsolutePath().startsWith(prefix));
// With a prefix, suffix and a parent
final String filePrefix = "prefix";
final String fileSuffix = ".tmp";
file = FileUtilities.createTempFile(filePrefix, fileSuffix, workingDirectory);
assertNotNull(file);
assertFalse(file.exists());
assertTrue(file.getAbsolutePath().startsWith(workingDirectory.getAbsolutePath()));
name = file.getName();
assertTrue(name.startsWith(filePrefix));
assertTrue(name.endsWith(fileSuffix));
assertEquals(filePrefix.length() + fileSuffix.length() + 4, name.length());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment