A File is an abstract path, it has no physical existence. It is only when “using” that File that the underlying physical storage is hit. When the file is getting created indirectly the abstract path is getting created. The file is one way, in which data will be stored as per requirement.
Type of File
- .tmp (Default File Type)
Primary, in order to create a temporary file, inbuilt files and functions are used which definitely will throw Exceptions here playing it safe. So in order to deal with it, we will be using Exception Handling Techniques. Here, we will use one of them known as- try-catch block techniques.
Secondary, additional work is simply we will be importing File Class for which we will be importing File Class.
Syntax: To import file library or Classes
import java.util.File ;
Syntax: To create a new file
File object_name = new File(Directory)
Syntax: To specify a directory is different in different operating systems (suppose java file is in a folder named ‘Folder’ is created on desktop)
In Linux and Mac
In Windows: ‘ \\ ‘ used instead of ‘ / ‘ to escape ‘ \ ‘ character. So the same directory is accessed as
A file that is temporary which in itself means should be supposed to be created just unlikely creating a new file and later on should wipe off as the command for deleting the file is called.
Approach: The standard method to create a temporary file in java is by using, For example, to create, to write, to compare two path names, to check whether a specific file is present or not, and many more. To understand this topic, first, consider one simple code as an example. So here task is subdivided into two parts. First, a new file should be created in the directory specified and the same file should be deleted in the same directory where it was created. Java provides numerous methods for handling files.
There are two standard methods for temporary file creation
Approach 1: File.createTempFile(String prefix, String suffix, File directory)
It is an Inbuilt standard method that is responsible for the creation of a temporary file. It creates a temporary file in the stated directory as specified on the local personal computer anywhere where there’s a permit to access. It takes 3 arguments namely prefix, suffix, and directory where the temporary file is supposed to be created
The parameters in this method are:
- Prefix: The prefix string is the name of the file.
- Suffix: The suffix string is the extension of the type of file that has to be created (Eg: .txt). However, if no arguments are given, .tmp will be the default file type.
- The file directory is the directory where the temporary file will be stored. ‘NULL’ has to be specified for using the default directory.
Example: Directory access does differ from operating systems. So for implementation mac ecosystem is taken into consideration so do the syntax for accessing the directory.
Basic Terminal commands
- Terminal Command used to compile any java code on the machine
- Terminal Command used to Run any java code on the machine
- javac class_name.java // For Compilation
- java class_name // For Execution
Terminal of Mac operating system will be used for implementation and providing an output of accessing the directory
Directory Used : /Users/mayanksolanki/Desktop/Folder/
Let us take an example to illustrate a temporary file creation in Java Program
String prefix =
String suffix =
File directoryPath =
File tempFile = File.createTempFile(prefix, suffix,
Ambiguity lies in the delete operation- tempFile.delete() // Refer to line number 27 in above code
The rate at which above java command executes is too faster that no file is seen in the directory window. Actually the file is created in the directory and deleted at the same time. Other proof can be if after file creation the programmer insert print statement “Temp file created ” than it will be displayed as the output but again no temporary file icon will be visible in the directory.
Visualizing output through instances even for a greater level of understanding. In the directory, while the code is getting executed :
Image1: Snapshot representing an instance when code is successfully compiled and till now code is not executed in the terminal
Image2: Snapshot representing an instance before the creation of any temporary file on the system
ls // The terminal command used here to check files inside the current folder
Image3: It should have been the actual output to be seen through eyeballs but it is not. Users will not be able to see any temporary file created in the specified directory (here folder) but it is created. The whole concept revolves around the speed of delete operation that is required at the end to terminate the code to make the file that is supposed to be created temporarily.
Let us take another example with slight improvements in the above approach to illustrate a temporary file in Java Program
File file = File.createTempFile(
Here, the program returns the pathway of the empty temporary file that is created in the specified directory and deletes the file on exit.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!