Open In App

Java Program to Write Data to Temporary File

Last Updated : 22 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Creating a temporary file on the default location
  2. 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:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads