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
- .txt
- .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
/Users/mayanksolanki/Desttop/Folder/
In Windows: ‘ \\ ‘ used instead of ‘ / ‘ to escape ‘ \ ‘ character. So the same directory is accessed as
\\Users\\mayanksolanki\\Desktop\\Folder\\
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
- File.createTempFile
- file.getAbsolutePath
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
Java
/// Java program to illustrate a temporary file creation import java.io.File; import java.io.IOException; public class GFG { // Main Method public static void main(String[] args) throws IOException { // Creating a string for the prefix String prefix = "exampleFile" ; // Creating a string for the suffix String suffix = ".txt" ; // Creating a File object for the directory path File directoryPath = new File( "/Users/mayanksolanki/Desktop/Folder/" ); // Creating the temporary file File tempFile = File.createTempFile(prefix, suffix, directoryPath); // Deleting the File after while exiting the // program(optional) tempFile.delete(); } } |
Output:
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.
Approach 2:
Let us take another example with slight improvements in the above approach to illustrate a temporary file in Java Program
Java
// Importing Classes/Files import java.io.File; public class GFG { // Main Driver Method public static void main(String[] args) throws Exception { // Creating the temporary file File file = File.createTempFile( "temp" , ".txt" , new File( " /Users/mayanksolanki/Desktop/Folder/" )); // Printing the path of the directory where the file // is created System.out.println(file.getAbsolutePath()); System.out.print(/Users/mayanksolanki/Desktop/Folder/); // Deleting the file while exiting the program file.deleteOnExit(); } } |
Output:
Here, the program returns the pathway of the empty temporary file that is created in the specified directory and deletes the file on exit.