Java Utililty Methods Temp File Create

List of utility methods to do Temp File Create

Description

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

Method

FilecreateTempFile(@Nonnull String prefix, @Nonnull String suffix, @Nullable File parentDir)
Creates a temporary file
Random rand = new Random();
String parent = parentDir == null ? System.getProperty("java.io.tmpdir") : parentDir.getPath();
DecimalFormat fmt = new DecimalFormat("#####");
File result;
do {
    result = new File(parent, prefix + fmt.format(Math.abs(rand.nextInt())) + suffix);
} while (result.exists());
return result;
...
FilecreateTempFile(final String file, final String ext)
create Temp File
File f = new File(file);
String filename = f.getName();
if (filename == null) {
    filename = "export";
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmm");
String date = formatter.format(new Date());
filename = filename + "_" + date + "_";
...
FilecreateTempFile(String oriFileName)
create Temp File
return new File(System.getProperty("java.io.tmpdir"),
        new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + "_" + new Random().nextInt(99999)
                + "_" + oriFileName);
FilegetTempFile()
get Temp File
File f = File.createTempFile("tmp", ".html", null);
tempFiles.add(f);
return f;
FilegetTempFile()
get Temp File
return File.createTempFile("temp", Long.toString(System.nanoTime()));
FilegetTempFile(File file)
create a empty temp file at file's directory, temp file's name is based on file's name, i.e.
File tempFile;
while (true) {
    tempFile = new File(file.getParent(), file.getName() + ".tmp." + FILENAME_RANDOM.nextInt());
    if (!tempFile.exists()) {
        if (!tempFile.createNewFile()) {
            throw new IOException(String.format("create %s failed", tempFile));
        return tempFile;
...
FilegetTempFile(File parent, String fileName)
Getting a temp file object in the directory in temporal directory which is referrenced by System.getProperty("java.io.tmpdir") .
return new File(new File(getTempDirectory(), parent.getPath()), fileName);
FilegetTempFile(final File stackFile)
get Temp File
return new File("/tmp/" + stackFile.getName() + ".tmp");
FilegetTempFile(final String directory, final String suffix, boolean autodelete)
Creates a temporary file in the given directory, with the given suffix.
int num = randomNumberGenerator.nextInt(Integer.MAX_VALUE);
File file = new File(directory + File.separator + String.format("%06d.%s", num, suffix));
while (file.exists()) {
    num = randomNumberGenerator.nextInt(Integer.MAX_VALUE);
    file = new File(directory + File.separator + String.format("%06d.%s", num, suffix));
if (!file.createNewFile()) {
    throw new IOException("Failed to create file " + file.getAbsolutePath());
...
FilegetTempFile(final String name)
get Temp File
return new File("temp" + File.separator + "test", name).getCanonicalFile();