Java-获取临时文件的路径

Java-获取临时文件的路径

Tags: Java

摘要

Java-获取临时文件的路径

获取文件临时路径例子

package cn.javacore.io.tempfile;

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

public class _04_GetTempFilePathExample {
    public static void main(String[] args) {

        try {

            //create a temp file
            File temp = File.createTempFile("temp-file-name", ".tmp");

            System.out.println("Temp file : " + temp.getAbsolutePath());

            //Get tempropary file path
            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();

        }

    }
}

输出结果

Temp file : /tmp/temp-file-name3605753746415045579.tmp
Temp file path : /tmp