Java IO Tutorial

How to get the temporary file path in Java

In Java, we can use System.getProperty("java.io.tmpdir") to get the default temporary file location.

  1. For Windows, the default temporary folder is %USER%\AppData\Local\Temp
  2. For Linux, the default temporary folder is /tmp

1. java.io.tmpdir

Run the below Java program on a Ubuntu Linux.

TempFilePath1

package com.mkyong.io.temp;

public class TempFilePath1 {

    public static void main(String[] args) {

        String tmpdir = System.getProperty("java.io.tmpdir");
        System.out.println("Temp file path: " + tmpdir);

    }

}

Output


Temp file path: /tmp

2. Create Temporary File

Alternatively, we can create a temporary file and substring the file path to get the temporary file location.

2.1 Java NIO example.

TempFilePath2.java

package com.mkyong.io.temp;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class TempFilePath2 {

    public static void main(String[] args) {

        // Java NIO
        try {
            Path temp = Files.createTempFile("", ".tmp");

            String absolutePath = temp.toString();
            System.out.println("Temp file : " + absolutePath);

            String separator = FileSystems.getDefault().getSeparator();
            String tempFilePath = absolutePath
                  .substring(0, absolutePath.lastIndexOf(separator));

            System.out.println("Temp file path : " + tempFilePath);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Output


Temp file : /tmp/log_11536339146653799756.tmp
Temp file path : /tmp

2.2 Java IO example.

TempFilePath3.java

package com.mkyong.io.temp;

import java.io.File;
import java.io.IOException;

public class TempFilePath3 {

    public static void main(String[] args) {

        // Java IO
        try {
            File temp = File.createTempFile("log_", ".tmp");
            System.out.println("Temp file : " + temp.getAbsolutePath());

            String absolutePath = temp.getAbsolutePath();
            String tempFilePath = absolutePath
                  .substring(0, absolutePath.lastIndexOf(File.separator));

            System.out.println("Temp file path : " + tempFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Output


Temp file : /tmp/log_9219838414378386507.tmp
Temp file path : /tmp

Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-io

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
7 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Nikita Menon
10 years ago

Why do we need both normal file and temporaray file?. What is the advantage of creating a temporary files ?

scf
10 years ago

public static void main(String[] args) throws IOException {
//if running w/o security manager
System.out.println(System.getProperty(“java.io.tmpdir”));

//if running with security manager
File f = File.createTempFile(“test”, null);
System.out.println(f.getParentFile().getAbsolutePath());
}

Charl FOurie
12 years ago

You can just do the following:

import java.io.File;

public class Blah {

  public static void main(String[] args) {
    File tempDir = new File(System.getProperty("java.io.tmpdir"));
    System.out.println("Temp File Path: " + tempDir);
  }

}
ashok
11 years ago

How to display that temp file?

vikrant
8 years ago

how to get path of folder by mouse double click on that folder

ashok
11 years ago

i am tring to display the created temp file as follows
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(mFile), contentType);

ashok
11 years ago
Reply to  ashok

but above one is not working