{"id":186,"date":"2009-07-27T21:46:46","date_gmt":"2009-07-27T20:46:46","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=186"},"modified":"2009-07-27T22:49:19","modified_gmt":"2009-07-27T21:49:19","slug":"readwrite-files","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/27\/readwrite-files\/","title":{"rendered":"Read\/Write Files"},"content":{"rendered":"<p><span id=\"zipfile\"><a href='http:\/\/www.codingfriends.com\/wp-content\/uploads\/2009\/07\/javareadfile.zip'><\/a><\/span>To read in from and file and also output to a file, java uses streams to &#8220;talk&#8221; to the files.  These streams allow for communicating with different stream conversion tools, the object tool within this tutorial is the DataInputStream which &#8220;talks&#8221; to the FileInputStream.<\/p>\n<p>I have attached the code within a zip file with the required input file as well.<\/p>\n<p>Below is the code<\/p>\n<pre lang=\"java\">\r\nimport java.io.*;\r\n\r\nclass javareadfile\r\n{\r\n       public static void main(String args[])\r\n       {\r\n              String fileName = \"country.txt\";\/\/input file\r\n              String outFileName = \"sqljavacountry.txt\";\/\/output file\r\n\r\n              try\r\n              {\r\n                     \/\/ file input stream, basically a pointer to the stream of the input file\r\n                     FileInputStream fStream = new FileInputStream(fileName);\r\n                     \/\/ file output stream, basically a pointer to the stream for outputing data\r\n                     FileOutputStream fOutStream = new FileOutputStream(outFileName);\r\n\r\n                     \/\/ the input\/output data streams that connect to the above streams\r\n                     DataInputStream dInput = new DataInputStream(fStream);\r\n                     DataOutputStream dOutput = new DataOutputStream(fOutStream);\r\n\r\n                     \/\/ whilst there is data available in the input stream\r\n                     while (dInput.available() !=0)\r\n                     {\r\n                            String in = dInput.readLine();\/\/ read a line from the input file\r\n                            \/\/ output a stream of data to the output file\r\n                            dOutput.writeBytes(\"insert into country(place) values (\\\"\"+in+\"\\\");\\n\");\r\n                     }\r\n                     \/\/close the two files.\r\n                     dInput.close();\r\n                     dOutput.close();\r\n              }\r\n              catch (Exception e)\/\/ incase of any errors\r\n              {\r\n                     System.err.println(\"There was a error : \" + e.toString());\r\n              }\r\n       }\r\n}\r\n<\/pre>\n<p>If you save this as javareadfile.java and also create a countrys.txt file with what ever text you like, e.g.<\/p>\n<pre class=\"consoleoutput\">\r\nUnited Kingdom\r\nFrance\r\nGermany\r\nUnited States\r\netc.\r\n<\/pre>\n<p>Once you have compiled the program (javac  -deprecation javareadfile.java, may have to use  -deprecation  if using java virtual machine 1.5 over 1.4) and execute the program (java javareadfile), it will look for the file countrys.txt within the same directory as the program executable and also create outcountrys.txt within the same directory (it will write over the file if there is one present).  Then within the while loop it will read the input file line by line and output to the screen what has been read and also output to the output file.<\/p>\n<p>Of course to finish off to close down the files and return back to the console.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To read in from and file and also output to a file, java uses streams to &#8220;talk&#8221; to the files. These streams allow for communicating with different stream conversion tools, the object tool within this tutorial is the DataInputStream which &#8220;talks&#8221; to the FileInputStream. I have attached the code within a zip file with the &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/27\/readwrite-files\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Read\/Write Files<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[401,398,400],"class_list":["post-186","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-linux","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=186"}],"version-history":[{"count":4,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":230,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/186\/revisions\/230"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}