| by Arround The Web | No comments

Java File CreateTempFile

“There are situations when we have to use the files and folders at some level whenever we have been working on any sort of operating system. Sometimes, you must create code files, text files, or folders to store those programming files. Any operating system must contain its hidden processing files or some temporary runtime files that are created at run time and would be removed from the system after a restart. The java programming lets you create the temporary files via its createTempFile() function from the File class, which you can remove after a while. This guide will demonstrate everything about the createTempFile() function.”

After the full fledge update of your Linux machine, a system user must be sure that the system has a “Java” environment already working on it. As the attached image portrays, we have installed the latest version “11.0.16” of Java Development Kit, i.e., using the version query.

Example 01

The very first illustration would contain the most basic java code to demonstrate the use of the createTempFile() function from the “File” class of Java to make temporary files. For this, we have been generating a “temp.java” file in the home folder casting off the “touch” query.

We have tried the text editor to open this file and successfully added the shown-below script. Starting from the import of the “java.io.File” package and the creation of a user-defined “FileDemo” class, we have added the main() function only. This function execution starts with initializing a File class object “file” as “null”. The try-catch statement always comes in handy when we are not sure about the authenticity of our code.

So, the main execution code has been added to the “try” part and created a new temporary file via the createTempFile() function call through the file object “file” and saved to the variable “file”. The prefix ”one” and suffix “.txt” has been provided along with the path to the directory in which it will be generated. The println() statement from java’s “System.out” class is here displaying the name of a file just created by calling the “getName()” function through object “file”. The catch statement would be displaying any exception that occurs throughout the program.

The execution of our java file after saving it returns the name of a temporary file generated. The “ls” instruction displays that it has been located in the home directory.

Example 02

The first example was very basic and easy to be used for new java users. Now, we will use a little enhanced example for our java users to increase their understanding level. This example would be started from the import of the same “java.io.File” package and the same class containing the same main() method. The execution of the main() function initiates with the same file object creation. The difference is here in the try part of this code.

We have been creating two temporary files using the same createTempFile() functions, i.e., “one” and “two” respectively. The first would contain the “.txt” suffix while the other one has a “null” suffix defined, i.e., the second would be using “.tmp” as its suffix. The getName() is called within the println() function to print out the names for both the files. The newest thing in this code is the use of the getAbsolutePath() function with the help of a “file” object in the System.out.println() function statement.

It has been used to generate the absolute path for the temporary file and display it on the shell screen. The catch statement is again here to display errors. Our java code has been ready for use after saving it by the “Ctrl+S”.

After executing the above-updated java code, we have been running it through the “java” execution command, i.e., using the name of a script file “temp.java”. The output shows the Names of two files and their absolute paths where these files reside on all separate lines. The “ls” instruction shows that both the files are in the home folder.

Example 03

Within the above two examples, we have displayed the use of the createTempFile() function to create a temporary file, the getName function to display its name, and the getAbsolutePath() function to display their exact path. As the newly created files are temporary, our Linux system will automatically remove them after a shutdown and a restart. But, if you want to delete these temporary files just after their creation, you can do so quickly by using java’s “deleteOnExit()” function from the java.io.File package.

This function would be deleting the just created temporary files from the system as quickly as the program terminates. Therefore, we have been using the very same code for this example with a slight change. We have been importing the same package, defining the same class and same main() function in it. The main() function has been starting from the file object initialization to null and using a try-catch statement for overall execution. We have added the very same steps to create a temporary file, to get its name and absolute path for display for both files. Additionally, we have added a new function to delete those files right after displaying their absolute path. i.e. “deleteOnExit()”.

On executing this program, both the temporary files got created successfully. Meanwhile, the use of the “ls” instruction shows that these files are deleted after the end of the program.

Conclusion

We have successfully discussed the variety of Java functions to work with files, especially using the createTempFile function through our code examples. We have elaborated a total of three examples utilizing the createTempFile() function to create temporary files, the getName() function to get and display the names of those temporary files, and the getAbsolutePath() method to show the path to that temporary file. In the end, we have also used the “deleteOnExit()” function of java to delete those temporary files right after the program exit.

Share Button

Source: linuxhint.com

Leave a Reply