{"id":20491,"date":"2022-09-15T23:01:22","date_gmt":"2022-09-15T17:31:22","guid":{"rendered":"https:\/\/java2blog.com\/?p=20491"},"modified":"2022-09-15T23:01:22","modified_gmt":"2022-09-15T17:31:22","slug":"get-temp-directory-path-java","status":"publish","type":"post","link":"https:\/\/java2blog.com\/get-temp-directory-path-java\/","title":{"rendered":"How to Get Temp Directory Path in Java"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Get_Temp_Directory_Path_in_Java\">Get Temp Directory Path in Java<\/a><ul><li><a href=\"#Using_SystemgetProperty\">Using System.getProperty()<\/a><\/li><li><a href=\"#By_Creating_Temp_File_and_Extracting_Temp_Path\">By Creating Temp File and Extracting Temp Path<\/a><ul><li><a href=\"#Using_javaioFile\">Using java.io.File<\/a><\/li><li><a href=\"#Using_javanioFileFiles\">Using java.nio.File.Files<\/a><\/li><\/ul><\/li><\/ul><\/li><li><a href=\"#Override_Default_Temp_Directory_Path\">Override Default Temp Directory Path<\/a><\/li><\/ul><\/div>\n<p>In this post, we will see how to get temp directory path in java.<\/p>\n<h2><span id=\"Get_Temp_Directory_Path_in_Java\">Get Temp Directory Path in Java<\/span><\/h2>\n<h3><span id=\"Using_SystemgetProperty\">Using System.getProperty()<\/span><\/h3>\n<p>To get the temp directory path, you can simply use <code>System.getProperty(&quot;java.io.tmpdir&quot;)<\/code>. It will return default temporary folder based on your operating system.<\/p>\n<blockquote>\n<p>Default temp directory path<br \/>\nWindows : <strong>%USER%\\AppData\\Local\\Temp<\/strong><br \/>\nLinux\/Unix: <strong>\/tmp<\/strong><\/p>\n<\/blockquote>\n<p>Let&#8217;s see with the help of example:<\/p>\n<pre code=\"java\" title = \"Get temp directory path in java using Syste.getProperty()\">\npackage org.arpit.java2blog;\n\npublic class GetTemporaryPathMain {\n\n    public static void main(String[] args) {\n        String tempDirPath = System.getProperty(\"java.io.tmpdir\");\n        System.out.println(\"Temp directory path : \" + tempDirPath);\n    }\n}\n<\/pre>\n<p>Output:<\/p>\n<pre title=\"Output\">\nTemp directory path : C:\\Users\\Arpit\\AppData\\Local\\Temp\\\n<\/pre>\n<h3><span id=\"By_Creating_Temp_File_and_Extracting_Temp_Path\">By Creating Temp File and Extracting Temp Path<\/span><\/h3>\n<p>We can also create temp file and extract temp directory path using String&#8217;s <a href=\"https:\/\/java2blog.com\/java-string-substring-example\/\" title=\"substring() method\">substring() method<\/a>.<\/p>\n<h4><span id=\"Using_javaioFile\">Using java.io.File<\/span><\/h4>\n<p>To get temp directory path:<\/p>\n<ul>\n<li>Use <code>File.createTempFile()<\/code> to create temp file.<\/li>\n<li>Get absolute path using <code>File&#039;s getAbsolutePath()<\/code> method.<\/li>\n<li>Use substring() method to extract temp directory path from absolute path.<\/li>\n<\/ul>\n<pre code=\"java\" title = \"Get temp directory path in java\">\npackage org.arpit.java2blog;\n\nimport java.io.File;\nimport java.io.IOException;\n\npublic class CreateFileAndGetTempDirMain {\n\n    public static void main(String[] args) {\n        try {\n            \/\/ Create temp file\n            File temp = File.createTempFile(\"temp_\", \".tmp\");\n\n            String absTempFilePath = temp.getAbsolutePath();\n            System.out.println(\"Temp file path : \" + absTempFilePath);\n\n            \/\/ Get temp directory path using substring method\n            String tempDirPath = absTempFilePath\n                    .substring(0, absTempFilePath.lastIndexOf(File.separator));\n\n            System.out.println(\"Temp directory path : \" + tempDirPath);\n        }\n        catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n}\n\n<\/pre>\n<p>Output:<\/p>\n<pre title=\"Output\">\nTemp file path : C:\\Users\\Arpit\\AppData\\Local\\Temp\\temp_10478314566038976912.tmp\nTemp directory path : C:\\Users\\Arpit\\AppData\\Local\\Temp\n<\/pre>\n<h4><span id=\"Using_javanioFileFiles\">Using java.nio.File.Files<\/span><\/h4>\n<p>To get temp directory path:<\/p>\n<ul>\n<li>Use <code>Files.createTempFile()<\/code> to create temp file.<\/li>\n<li>Get absolute path using <code>toString()<\/code> method.<\/li>\n<li>Use substring() method to extract temp directory path from absolute path.<\/li>\n<\/ul>\n<pre code=\"java\" title = \"Get temp directory path in java\">\npackage org.arpit.java2blog;\n\nimport java.io.IOException;\nimport java.nio.file.FileSystems;\nimport java.nio.file.Files;\nimport java.nio.file.Path;\n\npublic class CreateFileNIOAndGetTempDirMain {\n\n    public static void main(String[] args) {\n        try {\n            \/\/ Create temp file\n            Path temp = Files.createTempFile(\"temp_\", \".tmp\");\n\n            String absTempFilePath = temp.toString();\n            System.out.println(\"Temp file path : \" + absTempFilePath);\n\n            String fileSeparator = FileSystems.getDefault().getSeparator();\n            String tempDirPath = absTempFilePath\n                    .substring(0, absTempFilePath.lastIndexOf(fileSeparator));\n\n            System.out.println(\"Temp directory path : \" + tempDirPath);\n        }\n        catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n}\n\n<\/pre>\n<p>Output:<\/p>\n<pre title=\"Output\">\nTemp file path : C:\\Users\\Arpit\\AppData\\Local\\Temp\\temp_6905627814634776014.tmp\nTemp directory path : C:\\Users\\Arpit\\AppData\\Local\\Temp\n<\/pre>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/how-to-get-current-working-directory-in\/\" target=\"_blank\" rel=\"noopener\">How to get current working directory in java<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/how-to-get-current-working-directory-in\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/how-to-get-home-directory-in-java\/\" target=\"_blank\" rel=\"noopener\">How to get home directory in java<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/how-to-get-home-directory-in-java\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<h2><span id=\"Override_Default_Temp_Directory_Path\">Override Default Temp Directory Path<\/span><\/h2>\n<p>If you want to override temp directory path, you can run java program with JVM arguments as <code>-Djava.io.tmpdir=Temo_file_path<\/code><\/p>\n<p>For example:<br \/>\nIf you want set temp directory path as <code>C:\\temp<\/code>, you can run java program with JVM argument as <code>-Djava.io.tmpdir=C:\\temp<\/code><\/p>\n<p>That&#8217;s all about how to get temp directory path in Java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsGet Temp Directory Path in JavaUsing System.getProperty()By Creating Temp File and Extracting Temp PathUsing java.io.FileUsing java.nio.File.FilesOverride Default Temp Directory Path In this post, we will see how to get temp directory path in java. Get Temp Directory Path in Java Using System.getProperty() To get the temp directory path, you can simply use System.getProperty(&quot;java.io.tmpdir&quot;). [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[63,5,216],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20491"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=20491"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20491\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=20491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=20491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=20491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}