Your application has to write data out in a temporary file to reprocess them before rendering to users. The file will be created temporarily and swept our when the application restarts or shuts down. How do we create such a file?
Table of Contents
Introduction a context
In this tutorial, we will learn how to create a temporary file in Java. There are two static methods createTempFile()
in the Java File class, which will help to create temp file on the default TEMP folder location and another one is used to create temp file on specified folder location.
How to implement
I present an introductory implementation in some widely used languages. The detailed implementation could be slightly different.
Java
In below example, we have created two temp files. In the first method call, the temp file is created on the window’s default TEMP folder location. In the second method call, we have specified the directory in which the file is to be created.
package com.itersdesktop.javatechs; import java.io.File; import java.io.IOException; public class TempFileExample { public static void main(String[] args) { File tempFile = new File() try { File tempFile = File.createTempFile("my-data-file", ".dat"); System.out.println("Temp file On Default Location: " + tempFile.getAbsolutePath()); tempFile = File.createTempFile("my-data-file", ".dat", new File("C:/Users/tom/Temp")); System.out.println("Temp file On Specified Location: " + tempFile.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } finally { tempFile.deleteOnExit(); System.out.println("Exit!"); } } }
Groovy
Let’s have a look at an example below. It looks easy and explicit.
File statFile = File.createTempFile("organismStat", ".csv") statFile << "organism;count;normalisedCount\n" ... ... statFile.append "${tax['label']};${tax['count']};${normalisedCount}\\n" logger.info("The location of statistic file about organism is ${statFile.absolutePath}")
What we have used is the File.createTempFile
method of creating a file called myTempFile
which can be found at the location myTemplFile.absolutePath()
.
Python
To create a provisional file in Python3.7, we can use tempfile
module. This module creates temporary files and directories. It works on all supported platforms. TemporaryFile
, NamedTemporaryFile
, TemporaryDirectory
, and SpooledTemporaryFile
are high-level interfaces which provide automatic cleanup and can be used as context managers. mkstemp()
and mkdtemp()
are lower-level functions which require manual cleanup.
>>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed
Summary
I have shown you some sample implementations of creating a provisional file in Java, Groovy and Python. This tip could help you speed your work somehow. Happy coding!
References
- Generate temporary files and directories, accessed on 28/05/2020