{"id":2727,"date":"2017-11-28T05:54:54","date_gmt":"2017-11-28T05:54:54","guid":{"rendered":"http:\/\/webartdevelopers.com\/?p=2727"},"modified":"2017-11-28T05:54:54","modified_gmt":"2017-11-28T05:54:54","slug":"how-to-read-and-write-from-the-file-using-randomaccessfile-class-in-core-java","status":"publish","type":"post","link":"https:\/\/webartdevelopers.com\/blog\/how-to-read-and-write-from-the-file-using-randomaccessfile-class-in-core-java\/","title":{"rendered":"How to Read and Write from the File using RandomAccessFile class in Core Java"},"content":{"rendered":"<p>In this tutorial how to read and write from the file using RandomAccessFile class in Core Java\u00a0 is shown.<\/p>\n<p>Code:<\/p>\n<p>RandomAccessFileDemo.java<\/p>\n<p>import java.io.*;<\/p>\n<p>class RandomAccessFileDemo<br \/>\n{<br \/>\npublic static void main(String args[]) throws FileNotFoundException<br \/>\n{<br \/>\nFile f1 = new File(&#8220;Demo.txt&#8221;);<br \/>\nint a=123;<br \/>\nlong b=435523;<br \/>\nString s=&#8221;Here is some text&#8221;;<\/p>\n<p>try<br \/>\n{<br \/>\nRandomAccessFile raf = new RandomAccessFile(f1,&#8221;rw&#8221;);<br \/>\nraf.writeInt(a);<br \/>\nraf.writeLong(b);<br \/>\nraf.writeUTF(s);<br \/>\nraf.seek(0);<br \/>\nSystem.out.println(raf.read());<br \/>\nraf.close();<br \/>\n}<br \/>\ncatch(IOException ex)<br \/>\n{<br \/>\nSystem.out.println(ex.toString());<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p><a href=\"https:\/\/youtu.be\/dT1sp3GYHWk\">https:\/\/youtu.be\/dT1sp3GYHWk<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"In this tutorial how to read and write from the file using RandomAccessFile class in Core Java\u00a0 is shown. Code: RandomAccessFileDemo.java import java.io.*; class RandomAccessFileDemo { public static void main(String args) throws FileNotFoundException { File f1 = new File(&#8220;Demo.txt&#8221;); int a=123; long b=435523; String s=&#8221;Here is some text&#8221;; try { RandomAccessFile raf = new RandomAccessFile(f1,&#8221;rw&#8221;); raf.writeInt(a); raf.writeLong(b); raf.writeUTF(s); raf.seek(0); System.out.println(raf.read()); raf.close(); } catch(IOException ex) { System.out.println(ex.toString()); } } }<!-- AddThis Advanced Settings generic via filter on get_the_excerpt -->","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[156],"tags":[157,206,205,199,204],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2727"}],"collection":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/comments?post=2727"}],"version-history":[{"count":1,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2727\/revisions"}],"predecessor-version":[{"id":2728,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2727\/revisions\/2728"}],"wp:attachment":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/media?parent=2727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/categories?post=2727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/tags?post=2727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}