{"id":14587,"date":"2024-03-15T09:34:57","date_gmt":"2024-03-15T09:34:57","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/"},"modified":"2025-08-06T10:44:06","modified_gmt":"2025-08-06T10:44:06","slug":"how-to-create-a-txt-file-and-write-data-in-java","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/","title":{"rendered":"Java Write to File: Create &#038; Write Text Files"},"content":{"rendered":"<p>You can use the FileWriter class in Java to create and write to text files. Here is an example code to create a file named example.txt and write data to it:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">import<\/span> java.io.FileWriter;\r\n<span class=\"hljs-keyword\">import<\/span> java.io.IOException;\r\n\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">CreateAndWriteFile<\/span> {\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title function_\">main<\/span><span class=\"hljs-params\">(String[] args)<\/span> {\r\n        <span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">data<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-string\">\"Hello, World!\"<\/span>;\r\n\r\n        <span class=\"hljs-keyword\">try<\/span> {\r\n            <span class=\"hljs-type\">FileWriter<\/span> <span class=\"hljs-variable\">writer<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">FileWriter<\/span>(<span class=\"hljs-string\">\"example.txt\"<\/span>);\r\n            writer.write(data);\r\n            writer.close();\r\n            System.out.println(<span class=\"hljs-string\">\"\u6587\u4ef6\u521b\u5efa\u548c\u5199\u5165\u6210\u529f\uff01\"<\/span>);\r\n        } <span class=\"hljs-keyword\">catch<\/span> (IOException e) {\r\n            System.out.println(<span class=\"hljs-string\">\"\u53d1\u751f\u9519\u8bef\uff1a\"<\/span> + e.getMessage());\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>The code above first defines a string variable named data, which contains the data to be written to the file.<\/p>\n<p>Next, in the try block, we instantiate a FileWriter object and pass the filename &#8220;example.txt&#8221; to be created.<\/p>\n<p>Next, we utilize writer.write(data) to write the string data to the file.<\/p>\n<p>Finally, we closed the file writer and printed a successful message.<\/p>\n<p>If any IO exception occurs, we catch it in the catch block and print out the error message.<\/p>\n<p>Please note that this code will create and write to a new file. If a file named &#8220;example.txt&#8221; already exists, it will be overwritten. If you want to append data to the end of an existing file instead of replacing it, you can set the second boolean parameter of the FileWriter constructor to true as shown below:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-type\">FileWriter<\/span> <span class=\"hljs-variable\">writer<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">FileWriter<\/span>(<span class=\"hljs-string\">\"example.txt\"<\/span>, <span class=\"hljs-literal\">true<\/span>);\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can use the FileWriter class in Java to create and write to text files. Here is an example code to create a file named example.txt and write data to it: import java.io.FileWriter; import java.io.IOException; public class CreateAndWriteFile { public static void main(String[] args) { String data = &#8220;Hello, World!&#8221;; try { FileWriter writer = [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[19689,19687,19688,8126,14647],"class_list":["post-14587","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-file-i-o-java","tag-filewriter-tutorial","tag-java-create-file","tag-java-file-handling","tag-java-file-operations"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Write to File: Create &amp; Write Text Files - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn how to create text files and write data in Java using FileWriter with step-by-step code examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Write to File: Create &amp; Write Text Files\" \/>\n<meta property=\"og:description\" content=\"Learn how to create text files and write data in Java using FileWriter with step-by-step code examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T09:34:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-06T10:44:06+00:00\" \/>\n<meta name=\"author\" content=\"Ava Mitchell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ava Mitchell\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/\"},\"author\":{\"name\":\"Ava Mitchell\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64\"},\"headline\":\"Java Write to File: Create &#038; Write Text Files\",\"datePublished\":\"2024-03-15T09:34:57+00:00\",\"dateModified\":\"2025-08-06T10:44:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/\"},\"wordCount\":180,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"file I\/O Java\",\"FileWriter tutorial\",\"Java create file\",\"Java file handling\",\"Java file operations\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/\",\"name\":\"Java Write to File: Create & Write Text Files - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T09:34:57+00:00\",\"dateModified\":\"2025-08-06T10:44:06+00:00\",\"description\":\"Learn how to create text files and write data in Java using FileWriter with step-by-step code examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Write to File: Create &#038; Write Text Files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64\",\"name\":\"Ava Mitchell\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g\",\"caption\":\"Ava Mitchell\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/avamitchell\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Java Write to File: Create & Write Text Files - Blog - Silicon Cloud","description":"Learn how to create text files and write data in Java using FileWriter with step-by-step code examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Java Write to File: Create & Write Text Files","og_description":"Learn how to create text files and write data in Java using FileWriter with step-by-step code examples.","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T09:34:57+00:00","article_modified_time":"2025-08-06T10:44:06+00:00","author":"Ava Mitchell","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Ava Mitchell","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/"},"author":{"name":"Ava Mitchell","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64"},"headline":"Java Write to File: Create &#038; Write Text Files","datePublished":"2024-03-15T09:34:57+00:00","dateModified":"2025-08-06T10:44:06+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/"},"wordCount":180,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["file I\/O Java","FileWriter tutorial","Java create file","Java file handling","Java file operations"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/","name":"Java Write to File: Create & Write Text Files - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T09:34:57+00:00","dateModified":"2025-08-06T10:44:06+00:00","description":"Learn how to create text files and write data in Java using FileWriter with step-by-step code examples.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-create-a-txt-file-and-write-data-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Java Write to File: Create &#038; Write Text Files"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64","name":"Ava Mitchell","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g","caption":"Ava Mitchell"},"url":"https:\/\/www.silicloud.com\/blog\/author\/avamitchell\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=14587"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14587\/revisions"}],"predecessor-version":[{"id":158621,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14587\/revisions\/158621"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=14587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=14587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=14587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}