{"id":97,"date":"2016-01-16T06:06:25","date_gmt":"2016-01-16T06:06:25","guid":{"rendered":"http:\/\/kaizen-koka.com\/?p=97"},"modified":"2016-01-16T06:15:58","modified_gmt":"2016-01-16T06:15:58","slug":"file-stream-reader-and-writer","status":"publish","type":"post","link":"https:\/\/kaizen-koka.com\/?p=97","title":{"rendered":"File, Stream, Reader and Writer"},"content":{"rendered":"<p>\n\t&nbsp;\n<\/p>\n<p>\n\t<strong><span style=\"font-size:16px;\">Stream hierarchy<\/span><\/strong>\n<\/p>\n<p>\n\t<a href=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputStreams.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"96\" data-permalink=\"https:\/\/kaizen-koka.com\/?attachment_id=96\" data-orig-file=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputStreams.png?fit=710%2C456&amp;ssl=1\" data-orig-size=\"710,456\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"IO_InputOutputStreams\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputStreams.png?fit=300%2C193&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputStreams.png?fit=710%2C456&amp;ssl=1\" alt=\"IO_InputOutputStreams\" class=\"aligncenter size-full wp-image-96\" height=\"456\" src=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputStreams.png?resize=710%2C456\" width=\"710\" srcset=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputStreams.png?w=710&amp;ssl=1 710w, https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputStreams.png?resize=300%2C193&amp;ssl=1 300w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/a>\n<\/p>\n<p>\n\t<a href=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputReadersWriters.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"95\" data-permalink=\"https:\/\/kaizen-koka.com\/?attachment_id=95\" data-orig-file=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputReadersWriters.png?fit=652%2C387&amp;ssl=1\" data-orig-size=\"652,387\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"IO_InputOutputReadersWriters\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputReadersWriters.png?fit=300%2C178&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputReadersWriters.png?fit=652%2C387&amp;ssl=1\" alt=\"IO_InputOutputReadersWriters\" class=\"aligncenter size-full wp-image-95\" height=\"387\" src=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputReadersWriters.png?resize=652%2C387\" width=\"652\" srcset=\"https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputReadersWriters.png?w=652&amp;ssl=1 652w, https:\/\/i0.wp.com\/kaizen-koka.com\/wp-content\/uploads\/2016\/01\/IO_InputOutputReadersWriters.png?resize=300%2C178&amp;ssl=1 300w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/a>\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t<span style=\"font-size:16px;\"><strong>Programs:<\/strong><\/span>\n<\/p>\n<p>\n\t<em><span style=\"font-size:14px;\"><strong>File Creation<\/strong><\/span><\/em>\n<\/p>\n<pre class=\"brush:java;\">\r\n\/\/ 1 way of creating a file\r\n\t\tFile simpleFile = new File(\r\n\t\t\t\t&quot;C:\\\\Users\\\\cccc\\\\workspace\\\\ccccc-B3\\\\cccc.txt&quot;);\r\n                simpleFile ..createNewFile();\r\n\/\/ 2nd way of creating a file\r\n\t\t\tString fileName = &quot;cap.txt&quot;;\r\n\t\t\tString path = System.getProperty(&quot;user.dir&quot;);\r\n\t\t\tString absolutePath = path + File.separator + fileName;\r\n\r\n\t\t\tFile capitalFile = new File(absolutePath);\r\n\t\t\tcapitalFile.createNewFile();<\/pre>\n<p>\n\t<em><span style=\"font-size:14px;\"><strong>Read from a file<\/strong><\/span><\/em>\n<\/p>\n<pre class=\"brush:java;\">\r\n\/\/One way \r\ntry (FileInputStream fis = new FileInputStream(simpleFile);\r\n\t\t\t\tBufferedInputStream bis = new BufferedInputStream(fis);\r\n\t\t\t\tDataInputStream dis = new DataInputStream(bis);) {\r\n\r\nwhile (dis.available() != 0) {\r\n\t\t\t\tSystem.out.println(dis.readLine());\r\n\t\t\t}\r\n} catch (IOException oie) {\r\n\r\n\t\t}\r\n\r\n\/\/Alternative way\r\nFileReader fr = new FileReader(capitalFile);\r\n\t\t\tBufferedReader br = new BufferedReader(fr);\r\n\t\t\tString line;\r\n\r\n\t\t\twhile ((line = br.readLine()) != null) {\r\n\t\t\t\tSystem.out.println(line);\r\n\t\t\t}<\/pre>\n<p>\n\t<span style=\"font-size:14px;\"><em>Wirting on to a file<\/em><\/span>\n<\/p>\n<pre class=\"brush:java;\">\r\ntry {\r\n\t\t\tString additionalText = &quot;This information goes to cap file&quot;;\r\n\t\t\tString fileName = &quot;cap.txt&quot;;\r\n\t\t\tString path = System.getProperty(&quot;user.dir&quot;);\r\n\t\t\tString absolutePath = path + File.separator + fileName;\r\n\r\n\t\t\tFile capFile = new File(absolutePath);\r\n\r\n\t\t\tif (!capFile.exists()) {\r\n\t\t\t\tcapFile.createNewFile();\r\n\t\t\t}\r\n\t\t\tFileOutputStream fos = new FileOutputStream(capFile,true);\r\n\t\t    \/\/TODO Appending data in to existing file\r\n\t\t\tfos.write(additionalText.getBytes());\r\n\t\t\tfos.flush();\r\n\t\t\tfos.close();\r\n\t\t\t\r\n\t\t\t\/\/Another way\r\n\t\t\tFileWriter fw = new FileWriter(capFile, true);\r\n\t\t\tBufferedWriter bw = new BufferedWriter(fw);\r\n\t\t\tbw.write(&quot;\\nthrough Buffered writer. second time&quot;);\r\n\t\t\tbw.flush();\r\n\t\t\tbw.close();\r\n\t\t} catch (IOException e) {\r\n\t\t}<\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Stream hierarchy &nbsp; Programs: File Creation \/\/ 1 way of creating a file File simpleFile = new File( &quot;C:\\\\Users\\\\cccc\\\\workspace\\\\ccccc-B3\\\\cccc.txt&quot;); simpleFile ..createNewFile(); \/\/ 2nd way of creating a file String [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[3,2],"tags":[],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-java","category-technology"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p70lnf-1z","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=97"}],"version-history":[{"count":2,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":103,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions\/103"}],"wp:attachment":[{"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kaizen-koka.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}