Java Program to Write Data to Temporary File
Last Updated :
22 Feb, 2021
A Temporary file or a Temp file is a file that is created in order to hold information for some time while a file is being created or modified. After the successful execution of the program or after the program is closed, the temporary file is deleted. Other advantages of having a temporary file are that it helps in storing and moving data, managing settings, help recover lost data, and manage multiple users. Temporary data can be referred to as internet browser cookies, caches that delete after we close the browser or end a session, and deleting them free's up valuable space on our hard-disk and speed of the computer.
The only reason we need to create a temporary file in java is that we may require it in various situations such as while creating Unit tests, where we don't want to store the output of the intermediate Java Operations. But if the tests are completed we may delete them as they may unnecessarily occupy space on the computer.
There are two methods that can be used to create temporary files as follows:
- Creating a temporary file on the default location
- Creating a temporary file on the specified location
Method 1: The temporary file is created on the default TEMP folder location.
Syntax:
File.createTempFile(String prefix, String suffix) throws IOException
Method 2: The temporary file is created on the Specified directory.
Syntax:
File.createTempFile(String prefix, String suffix, File directory) throws IOException
Implementation: Both the methods into a Java program later on analyzing the output
Example 1:
Java
import java.io.File;
import java.io.IOException;
public class GeeksforGeeks {
public static void main(String[] args)
{
try {
// To create a temp file on Default location:
File tempFile
= File.createTempFile("Hello Geek", ".tmp");
System.out.println(
"Temporary file is located on Default location"
+ tempFile.getAbsolutePath());
// To create a temp file on specified directory
tempFile = File.createTempFile(
"Hello Geek", ".tmp",
new File("/Users/ashray"));
System.out.println(
"Temporary file is located on Specified location: "
+ tempFile.getAbsolutePath());
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Console output:
Output explanation:
As we are now accustomed to the procedure of creating a Temporary file, we will now use it to write data into the Temporary file. The most efficient way to write characters into a temporary file will be by using Java BufferedWriter Class. It is used to provide buffering for writer instances and makes the performance fast.
Implementation: Writing data into a Temporary file
Example 2:
Java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class GeeksforGeeks {
public static void main(String[] args)
{
File tempFile = null;
BufferedWriter writer = null;
try {
// Creating a temporary file
tempFile
= File.createTempFile("MyTempFile", ".tmp");
writer = new BufferedWriter(
new FileWriter(tempFile));
writer.write(
"Hello Geek!! How was your day like ?, This is a temp file.");
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (writer != null)
writer.close();
}
catch (Exception ex) {
}
}
System.out.println(
"Hello Geek! Data has been stored in temporary file.");
System.out.println("Temp file location: "
+ tempFile.getAbsolutePath());
}
}
Output:
The console output is as follows
The 'Temp file' output is as follows:
Similar Reads
Java Program to Create a Temporary File 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 Fi
6 min read
Java Program to Write into a File FileWriter class in Java is used to write character-oriented data to a file as this class is character-oriented because it is used in file handling in Java. There are many ways to write into a file in Java as there are many classes and methods which can fulfill the goal as follows:Using writeString(
5 min read
Java Program to Read a File to String There are multiple ways of writing and reading a text file. This is required while dealing with many applications. There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader or Scanner to read a text file. Given a text file, the task is to read the contents
8 min read
Java Program to Save a String to a File A demo file on the desktop named 'gfg.txt' is used for reference as a local directory on the machine. Creating an empty file before writing a program and give that specific path of that file to the program. Methods: Using writeString() method of Files classUsing write() method of Files classUsing wr
6 min read
How to Delete Temporary File in Java? In java, we have a java.io package that provides various methods to perform various operations on files/directories. Temporary files are those files that are created for some business logic or unit testing, and after using these files you have to make sure that these temporary files should be delet
2 min read
Java Program to Make a File Read-Only Read-Only is the file attribute that the operating system assigns to the file. When the file is flagged as read-only, it means that it can only be opened or read, one cannot change the name of the file, can not rewrite or append the content of the file, and also cannot delete the file. Method 1: Usi
2 min read