{"id":158,"date":"2019-01-24T03:58:38","date_gmt":"2019-01-24T03:58:38","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/java\/?p=158"},"modified":"2020-08-06T09:54:49","modified_gmt":"2020-08-06T09:54:49","slug":"26-java-reading-writing-to-files","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/","title":{"rendered":"Java &#8211; Reading &#038; Writing to Files"},"content":{"rendered":"<p>We have covered <a href=\"https:\/\/www.kindsonthegenius.com\/java\/java-streams\/\">Streams(Input Stream and OuputStream)<\/a> in the previous lesson. We also treated Standard Streams.\u00a0 Now, you will learn about FileInputStream and FileOutputStream. You use these classes to read and write to files.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Content<\/strong><\/p>\n<ol>\n<li><a href=\"#t1\">FileInputStream<\/a><\/li>\n<li><a href=\"#t2\">FileOutputStream<\/a><\/li>\n<li><a href=\"#t3\">Program to Write to a File<\/a><\/li>\n<li><a href=\"#t4\">Program to Read from a File<\/a><\/li>\n<li><a href=\"#t5\">Watch the Video<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. FileInputStream<\/strong><\/h4>\n<p>You use this\u00a0 stream to read data from files. But before you can read data, you need to create a new FileInputStream object. The constructor take a the filepath as parameter. This is shown below<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">     InputStream fs <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> FileInputStream<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"C:\/programs\/test.txt\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n<\/pre>\n<p>So this code creates a new InputStream object fs. The file specified in the constructor is the file you want to read from.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Another Method<\/strong><\/p>\n<p>You can also create an InputStream object by first creating a file object . Then you pass the file object to the constructor of the FileInputStream. This is shown below:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">    File myfile <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> File<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"C:\/programs\/test.txt\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    InputStream fs <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> FileInputStream<span style=\"color: #333333;\">(<\/span>myfile<span style=\"color: #333333;\">);<\/span>\r\n<\/pre>\n<p>If you have created an InputStream object, then you have a number of helper methods you can use. These methods would help you read data from the file of perform other operations<\/p>\n<p>&nbsp;<\/p>\n<table class=\"table table-bordered\" align=\"center\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>SN<\/th>\n<th>Method brief description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>close() throws IOException{}<\/b><\/p>\n<p>You use this method to close the file output stream. It also releases any system resources associated with the file. It throws an IOException if something goes wrong.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>finalize() throws IOException {}<\/b><\/p>\n<p>This method is used to clean up the connection to the file. It ensures that the close method of the stream is called when there are no more references to the stream. Also throws an IOException.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>read(int r) throws IOException{}<\/b><\/p>\n<p>You use this method to read\u00a0 the specified byte of data from the InputStream. It returns an int value. Returns the next byte of data. But\u00a0 -1 is returned if it&#8217;s the end of the file.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>read(byte[] r) throws IOException{}<\/b><\/p>\n<p>You use this method to read r.length bytes from the input stream into a byte array. It returns the total number of bytes read. It returns -1 if it is end of file.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">5<\/td>\n<td><b>available() throws IOException{}<\/b><\/p>\n<p>It it used to get the number of byte that can be read from this file input stream. Returns an int value<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Table 1.0: Method Available for FileInputStream<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. FileOutputStream<\/strong><\/h4>\n<p>Unlike the FileInputStream, you use this stream\u00a0 to create a write to a file. You can also use it to create a file. So if a specified file does not exist, it can create it before writing to it.<\/p>\n<p>But before writing, the file has to be opened.<\/p>\n<p>To write to a file, you can use one of two methods:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Method 1:<\/strong> Create a FileOutputStream object and give it the filepath. This is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">   OutputStream fos <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> FileOutputStream<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"C:\/data\/out.txt\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Method 2:<\/strong> Create a file object. Then pass the file object as parameter to the FileOutpuStream constructor. For example, see below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">   File outfile <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> File<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"C:\/data\/out.txt\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n   OutputStream fos <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> FileOutputStream<span style=\"color: #333333;\">(<\/span>outfile<span style=\"color: #333333;\">);<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Just like FileInputStream, there are also methods available for FileOutputStream. Therefore, you use these methods to write data to file and perform other file operations. The list of method is provided below in Table 1.1. You probably should attempt to used them. The <a href=\"https:\/\/www.youtube.com\/watch?v=9AdGR9IZ3zg\">video lesson<\/a> can help you.<\/p>\n<p>&nbsp;<\/p>\n<table class=\"table table-bordered\" align=\"center\">\n<tbody>\n<tr style=\"background-color: #f7f6f3;\">\n<th>SN<\/th>\n<th>Method and brief description<\/th>\n<\/tr>\n<tr>\n<td class=\"ts\">1<\/td>\n<td><b>close() throws IOException{}<\/b><\/p>\n<p>You use this method to close\u00a0 the stream. It also releases any system resources associated with the file. It throws an IOException.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">2<\/td>\n<td><b>\u00a0finalize() throws IOException {}<\/b><\/p>\n<p>You use this method to up the connection to the file. Ensures that the close method of this file output stream is called when there are no more references to this stream. Throws an IOException.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">3<\/td>\n<td><b>write(int w) throws IOException{}<\/b><\/p>\n<p>You use this method to write the given byte to the output stream.<\/td>\n<\/tr>\n<tr>\n<td class=\"ts\">4<\/td>\n<td><b>write(byte[] w)<\/b><\/p>\n<p>This method also writes w.length bytes from the specified byte array to the OutputStream.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Table 1.1: Methods for OutputStream<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Program to Write to File<\/strong><\/h4>\n<p>We would write a program to create a file and write a list of items to the file. So we would use three classes:<\/p>\n<ul>\n<li><strong>File:<\/strong> Creates a file\u00a0 object taking the filepath as parameter<\/li>\n<li><strong>OutputStream<\/strong>: Creates a FileOutputStream object taking a File object as parameter<\/li>\n<li><strong>PrintWirter<\/strong>: This is use to write data so that i could be readable. It takes the FileOutputStream object as parameter<\/li>\n<\/ul>\n<p>Find the code below. Also try to run it yourself and get used to it.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.io.*<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">FileStreamDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">try<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t<span style=\"color: #333399; font-weight: bold;\">byte<\/span><span style=\"color: #333333;\">[]<\/span> byteToWrite <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">77<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">43<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">78<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">23<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">4<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">6<\/span><span style=\"color: #333333;\">};<\/span>\r\n\t\t\t\r\n\t\t\tFile ofile <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> File<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"D:\/data\/javadata.txt\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\t\r\n\t\t\tOutputStream ofs <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> FileOutputStream<span style=\"color: #333333;\">(<\/span>ofile<span style=\"color: #333333;\">);<\/span>\r\n\t\t\t\r\n\t\t\tPrintWriter ps <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> PrintWriter<span style=\"color: #333333;\">(<\/span>ofs<span style=\"color: #333333;\">);<\/span>\r\n\t\t\t\t\t\t\r\n\t\t\t<span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">byte<\/span> item <span style=\"color: #333333;\">:<\/span> byteToWrite<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\tps<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>item<span style=\"color: #333333;\">);<\/span>\r\n\t\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t\t\r\n\t\t\tps<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">close<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\t\tofs<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">close<\/span><span style=\"color: #333333;\">();<\/span>\t\t\t\r\n\t\t<span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">catch<\/span> <span style=\"color: #333333;\">(<\/span>IOException e<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\te<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">printStackTrace<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.0: Program to write to a file<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4 id=\"t4\"><strong>4. Program to Read from a File<\/strong><\/h4>\n<p>Similar to writing to a file, we are going to read from a file. So we would read the content of the file and display it to the console.<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">java.io.*<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">FileStreamDemo<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\r\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">try<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\tFile infile <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> File<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"D:\/data\/javadata.txt\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t\t\tInputStream ifs <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> FileInputStream<span style=\"color: #333333;\">(<\/span>infile<span style=\"color: #333333;\">);<\/span>\r\n\t\t\t\r\n\t\t\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> size <span style=\"color: #333333;\">=<\/span> ifs<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">available<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\t\t\r\n\t\t\t<span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> i <span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">;<\/span> i<span style=\"color: #333333;\">&lt;<\/span>size<span style=\"color: #333333;\">;<\/span> i<span style=\"color: #333333;\">++)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>ifs<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">read<\/span><span style=\"color: #333333;\">());<\/span>\r\n\t\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t\t\r\n\t\t\tifs<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">close<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\t<span style=\"color: #333333;\">}<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">catch<\/span><span style=\"color: #333333;\">(<\/span>IOException e<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t\t\te<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">printStackTrace<\/span><span style=\"color: #333333;\">();<\/span>\r\n\t\t<span style=\"color: #333333;\">}<\/span>\t<span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.1: Program to Read the content of a file<\/p>\n<p>&nbsp;<\/p>\n<p>I recommend you also watch the video to get understand it better.<\/p>\n<h4><strong id=\"t5\">5. Watch the Video<\/strong><\/h4>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/e9JluwKBDwQ\" width=\"100%\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">\ufeff<\/span><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have covered Streams(Input Stream and OuputStream) in the previous lesson. We also treated Standard Streams.\u00a0 Now, you will learn about FileInputStream and FileOutputStream. You &hellip; <\/p>\n","protected":false},"author":395,"featured_media":159,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-158","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java - Reading &amp; Writing to Files - Java Tutorials<\/title>\n<meta name=\"description\" content=\"In this tutorial, we would learn how to read and write to files in Java. We&#039;ll learn about Input and Output Streams as well as PrintWriter\" \/>\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.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java - Reading &amp; Writing to Files - Java Tutorials\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, we would learn how to read and write to files in Java. We&#039;ll learn about Input and Output Streams as well as PrintWriter\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/\" \/>\n<meta property=\"og:site_name\" content=\"Java Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-24T03:58:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-06T09:54:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Reading-and-Writting-to-Files.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"950\" \/>\n\t<meta property=\"og:image:height\" content=\"518\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/26-java-reading-writing-to-files\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/26-java-reading-writing-to-files\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Java &#8211; Reading &#038; Writing to Files\",\"datePublished\":\"2019-01-24T03:58:38+00:00\",\"dateModified\":\"2020-08-06T09:54:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/26-java-reading-writing-to-files\\\/\"},\"wordCount\":784,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/26-java-reading-writing-to-files\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Reading-and-Writting-to-Files.jpg\",\"articleSection\":[\"Java Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/26-java-reading-writing-to-files\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/26-java-reading-writing-to-files\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/26-java-reading-writing-to-files\\\/\",\"name\":\"Java - Reading & Writing to Files - Java Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/26-java-reading-writing-to-files\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/26-java-reading-writing-to-files\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Reading-and-Writting-to-Files.jpg\",\"datePublished\":\"2019-01-24T03:58:38+00:00\",\"dateModified\":\"2020-08-06T09:54:49+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"description\":\"In this tutorial, we would learn how to read and write to files in Java. We'll learn about Input and Output Streams as well as PrintWriter\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/26-java-reading-writing-to-files\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/26-java-reading-writing-to-files\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/26-java-reading-writing-to-files\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Reading-and-Writting-to-Files.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Reading-and-Writting-to-Files.jpg\",\"width\":950,\"height\":518,\"caption\":\"Reading and Writing to Files in Java\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/26-java-reading-writing-to-files\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java &#8211; Reading &#038; Writing to Files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\",\"name\":\"Java Tutorials\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java - Reading & Writing to Files - Java Tutorials","description":"In this tutorial, we would learn how to read and write to files in Java. We'll learn about Input and Output Streams as well as PrintWriter","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.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/","og_locale":"en_US","og_type":"article","og_title":"Java - Reading & Writing to Files - Java Tutorials","og_description":"In this tutorial, we would learn how to read and write to files in Java. We'll learn about Input and Output Streams as well as PrintWriter","og_url":"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/","og_site_name":"Java Tutorials","article_published_time":"2019-01-24T03:58:38+00:00","article_modified_time":"2020-08-06T09:54:49+00:00","og_image":[{"width":950,"height":518,"url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Reading-and-Writting-to-Files.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Java &#8211; Reading &#038; Writing to Files","datePublished":"2019-01-24T03:58:38+00:00","dateModified":"2020-08-06T09:54:49+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/"},"wordCount":784,"commentCount":0,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Reading-and-Writting-to-Files.jpg","articleSection":["Java Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/","url":"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/","name":"Java - Reading & Writing to Files - Java Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Reading-and-Writting-to-Files.jpg","datePublished":"2019-01-24T03:58:38+00:00","dateModified":"2020-08-06T09:54:49+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"description":"In this tutorial, we would learn how to read and write to files in Java. We'll learn about Input and Output Streams as well as PrintWriter","breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Reading-and-Writting-to-Files.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Reading-and-Writting-to-Files.jpg","width":950,"height":518,"caption":"Reading and Writing to Files in Java"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/java\/26-java-reading-writing-to-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/java\/"},{"@type":"ListItem","position":2,"name":"Java &#8211; Reading &#038; Writing to Files"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/java\/#website","url":"https:\/\/www.kindsonthegenius.com\/java\/","name":"Java Tutorials","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/java\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/java\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":4,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"predecessor-version":[{"id":212,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/158\/revisions\/212"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/159"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}