{"id":696,"date":"2022-02-16T17:47:09","date_gmt":"2022-02-16T22:47:09","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=696"},"modified":"2022-02-16T17:47:11","modified_gmt":"2022-02-16T22:47:11","slug":"java-file-i-o","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/java-file-i-o\/","title":{"rendered":"Java file I\/O"},"content":{"rendered":"\n<p>Java provides a number of classes and methods that allow to read and write files. In Java all files are byte-oriented. Two of the most often-used stream classes are FileInputStream and FileOutputStream, which create byte streams linked to files.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>To open a file, simple create an object of one of the classes specifying the name of the file as an argument to the constructor.<\/li><li>Use close() to close the file<\/li><\/ol>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FileInputStream (String fileName)\nFileOutputStream (String fileName)<\/code><\/pre>\n\n\n\n<p>fileName specifies the name of the file that you want to open. After completing your file operations, you should close it by calling close(). It is defined by both FileInputStream and FileInputStream , as shown here:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void close() throws IOException<\/code><\/pre>\n\n\n\n<p>To read from a file, you can use a version of read() that is defined within FileInputStream, as shown here:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void close() throws IOException<\/code><\/pre>\n\n\n\n<p>Example of File Reading Code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.*;\n\nclass Variables\n{\n   public static void main(String&#91;] args) throws IOException {\n   int i;\n\n   FileInputStream fin;\n   File file = new File(\"\/home\/linuxjunky\/Documents\/Protein1\");    \n\n   try {\n       fin=new FileInputStream(file);\n   } catch (FileNotFoundException e) {\n       System.out.println(\"File Not Found\");\n       return;\n   } catch (ArrayIndexOutOfBoundsException e) {\n       System.out.println(\"Out of Bounds\");\n       return;\n   }\n   int j = 0;\n\n   int L = (int) file.length();\n   char&#91;] Array = new char&#91;L];\n   System.out.println(\"Th Length of The Protein Is: \" + L);\n   do \n   {\n       i = fin.read();\n\n       if (i != -1)\n       {    \n           Array&#91;j] = (char) i;\n           System.out.print(Array&#91;j++]);\n       }\n   } while(i != -1);\n   fin.close();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java provides a number of classes and methods that allow to read and write files. In Java all files are byte-oriented. Two of the most often-used stream classes are FileInputStream and FileOutputStream, which create byte streams linked to files. To open a file, simple create an object of one of the classes specifying the name [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-696","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/696","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=696"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/696\/revisions"}],"predecessor-version":[{"id":697,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/696\/revisions\/697"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}