{"id":3508,"date":"2020-05-29T08:00:58","date_gmt":"2020-05-29T07:00:58","guid":{"rendered":"https:\/\/www.itersdesktop.com\/?p=3508"},"modified":"2020-05-28T16:17:36","modified_gmt":"2020-05-28T15:17:36","slug":"how-to-create-a-temporary-file-to-store-data","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/29\/how-to-create-a-temporary-file-to-store-data\/","title":{"rendered":"How to create a temporary file to store data"},"content":{"rendered":"<p><\/p>\n<p class=\"has-drop-cap\">Your application has to write data out in a temporary file to reprocess them before rendering to users. The file will be created temporarily and swept our when the application restarts or shuts down. How do we create such a file?<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3ce6bc091f5\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3ce6bc091f5\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/29\/how-to-create-a-temporary-file-to-store-data\/#introduction-a-context\" >Introduction a context<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/29\/how-to-create-a-temporary-file-to-store-data\/#how-to-implement\" >How to implement<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/29\/how-to-create-a-temporary-file-to-store-data\/#java\" >Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/29\/how-to-create-a-temporary-file-to-store-data\/#groovy\" >Groovy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/29\/how-to-create-a-temporary-file-to-store-data\/#python\" >Python<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/29\/how-to-create-a-temporary-file-to-store-data\/#summary\" >Summary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.itersdesktop.com\/vi\/2020\/05\/29\/how-to-create-a-temporary-file-to-store-data\/#references\" >References<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"introduction-a-context\"><\/span>Introduction a context<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>In this tutorial, we will learn how to create a temporary file in Java. There are two static methods <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">createTempFile()<\/code> in the Java File class, which will help to create temp file on the default TEMP folder location and another one is used to create temp file on specified folder location.<\/p>\n<h1><span class=\"ez-toc-section\" id=\"how-to-implement\"><\/span>How to implement<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>I present an introductory implementation in some widely used languages. The detailed implementation could be slightly different.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"java\"><\/span>Java<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In below example, we have created two temp files. In the first method call, the temp file is created on the window&#8217;s default TEMP folder location. In the second method call, we have specified the directory in which the file is to be created.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">package com.itersdesktop.javatechs;\r\n \r\nimport java.io.File;\r\nimport java.io.IOException;\r\n \r\npublic class TempFileExample {\r\n    public static void main(String[] args) {\r\n        File tempFile = new File()\r\n        try {\r\n            File tempFile = File.createTempFile(\"my-data-file\", \".dat\");\r\n            System.out.println(\"Temp file On Default Location: \" + tempFile.getAbsolutePath());\r\n            tempFile = File.createTempFile(\"my-data-file\", \".dat\", new File(\"C:\/Users\/tom\/Temp\"));\r\n            System.out.println(\"Temp file On Specified Location: \" + tempFile.getAbsolutePath());\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        } finally {\r\n            tempFile.deleteOnExit();\r\n            System.out.println(\"Exit!\"); \r\n        }\r\n    }\r\n}<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"groovy\"><\/span>Groovy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let&#8217;s have a look at an example below. It looks easy and explicit.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">File statFile = File.createTempFile(\"organismStat\", \".csv\")\r\nstatFile &lt;&lt; \"organism;count;normalisedCount\\n\"\r\n...\r\n...\r\nstatFile.append \"${tax['label']};${tax['count']};${normalisedCount}\\\\n\"\r\nlogger.info(\"The location of statistic file about organism is ${statFile.absolutePath}\")\r\n<\/pre>\n<p>What we have used is the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">File.createTempFile<\/code> method of creating a file called <code class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">myTempFile<\/code> which can be found at the location <code class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">myTemplFile.absolutePath()<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"python\"><\/span>Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To create a provisional file in Python3.7, we can use <code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">tempfile<\/code> module. This module creates temporary files and directories. It works on all supported platforms.\u00a0<a class=\"reference internal\" title=\"tempfile.TemporaryFile\" href=\"https:\/\/docs.python.org\/3.7\/library\/tempfile.html#tempfile.TemporaryFile\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TemporaryFile<\/span><\/code><\/a>,\u00a0<a class=\"reference internal\" title=\"tempfile.NamedTemporaryFile\" href=\"https:\/\/docs.python.org\/3.7\/library\/tempfile.html#tempfile.NamedTemporaryFile\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">NamedTemporaryFile<\/span><\/code><\/a>,\u00a0<a class=\"reference internal\" title=\"tempfile.TemporaryDirectory\" href=\"https:\/\/docs.python.org\/3.7\/library\/tempfile.html#tempfile.TemporaryDirectory\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">TemporaryDirectory<\/span><\/code><\/a>, and\u00a0<a class=\"reference internal\" title=\"tempfile.SpooledTemporaryFile\" href=\"https:\/\/docs.python.org\/3.7\/library\/tempfile.html#tempfile.SpooledTemporaryFile\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">SpooledTemporaryFile<\/span><\/code><\/a>\u00a0are high-level interfaces which provide automatic cleanup and can be used as context managers.\u00a0<a class=\"reference internal\" title=\"tempfile.mkstemp\" href=\"https:\/\/docs.python.org\/3.7\/library\/tempfile.html#tempfile.mkstemp\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">mkstemp()<\/span><\/code><\/a>\u00a0and\u00a0<a class=\"reference internal\" title=\"tempfile.mkdtemp\" href=\"https:\/\/docs.python.org\/3.7\/library\/tempfile.html#tempfile.mkdtemp\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">mkdtemp()<\/span><\/code><\/a>\u00a0are lower-level functions which require manual cleanup.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">&gt;&gt;&gt; import tempfile\r\n\r\n# create a temporary file and write some data to it\r\n&gt;&gt;&gt; fp = tempfile.TemporaryFile()\r\n&gt;&gt;&gt; fp.write(b'Hello world!')\r\n# read data from file\r\n&gt;&gt;&gt; fp.seek(0)\r\n&gt;&gt;&gt; fp.read()\r\nb'Hello world!'\r\n# close the file, it will be removed\r\n&gt;&gt;&gt; fp.close()\r\n\r\n# create a temporary file using a context manager\r\n&gt;&gt;&gt; with tempfile.TemporaryFile() as fp:\r\n...     fp.write(b'Hello world!')\r\n...     fp.seek(0)\r\n...     fp.read()\r\nb'Hello world!'\r\n&gt;&gt;&gt;\r\n# file is now closed and removed\r\n\r\n# create a temporary directory using the context manager\r\n&gt;&gt;&gt; with tempfile.TemporaryDirectory() as tmpdirname:\r\n...     print('created temporary directory', tmpdirname)\r\n&gt;&gt;&gt;\r\n# directory and contents have been removed<\/pre>\n<h1><span class=\"ez-toc-section\" id=\"summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>I have shown you some sample implementations of creating a provisional file in Java, Groovy and Python. This tip could help you speed your work somehow. Happy coding!<\/p>\n<h1><span class=\"ez-toc-section\" id=\"references\"><\/span>References<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<ol>\n<li><a href=\"https:\/\/docs.python.org\/3.7\/library\/tempfile.html\" target=\"_blank\" rel=\"noopener noreferrer\">Generate temporary files and directories<\/a>, accessed on 28\/05\/2020<\/li>\n<\/ol>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>Your application has to write data out in a temporary file to reprocess them before rendering to users. The file will be created temporarily and swept our when the application&hellip; <\/p>\n","protected":false},"author":2,"featured_media":3548,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[348,196,595,28,122],"tags":[756],"class_list":["post-3508","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structures","category-file-type","category-groovy","category-java","category-python","tag-create-a-file"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/3508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/comments?post=3508"}],"version-history":[{"count":6,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/3508\/revisions"}],"predecessor-version":[{"id":3549,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/3508\/revisions\/3549"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media\/3548"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media?parent=3508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/categories?post=3508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/tags?post=3508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}