{"id":90028,"date":"2024-06-17T18:00:43","date_gmt":"2024-06-17T12:30:43","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=90028"},"modified":"2024-06-17T18:09:00","modified_gmt":"2024-06-17T12:39:00","slug":"java-filereader-class","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/","title":{"rendered":"Java FileReader Class with Examples"},"content":{"rendered":"<p>Using Files or Blob objects to specify the file or data you want to read, the FileReader object allows Web applications to read the contents of files or raw data buffers stored by a user&#8217;s computer.<\/p>\n<h2>Here are some key points about FileReader in Java<\/h2>\n<ul>\n<li>Reading character data<\/li>\n<li>Efficient text reading<\/li>\n<li>Use with text files<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">try (FileReader reader = new FileReader(\"sample.txt\")) {\r\n    int character;\r\n    while ((character = reader.read()) != -1) {\r\n        System.out.print((char) character);\r\n    }\r\n} catch (IOException e) {\r\n    e.printStackTrace();\r\n}<\/pre>\n<h3>Benefits:<\/h3>\n<ul>\n<li>Character-Oriented<\/li>\n<li>Efficient for Text Files<\/li>\n<li>Exception HandlingC<\/li>\n<li>Convenient for Line-Based Reading<\/li>\n<\/ul>\n<p>The FileReader property allows web applications, using Files or Blob Objects to specify a file or data that they will open in an asynchronous manner, to access contents of files and Raw Data buffers from the user&#8217;s computer.<\/p>\n<p><strong>Example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.*;\r\nclass TechVidvan{\r\n  public static void main(String[] args)\r\n  {\r\n    try {\r\n      FileReader fileReader= new FileReader(\"output.txt\");\r\n\r\n      System.out.println(\"Text in tutorial: \\n\");\r\n      int i;\r\n      while ((i = fileReader.read()) != -1) {\r\n        System.out.print((char)i);\r\n      }\r\n    }\r\n\r\n    catch (Exception e) {\r\n      System.out.println(e);\r\n    }\r\n  }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nText in tutorial:<br \/>\nwelcome to TechVidvan Java Course<\/p>\n<h3>FileReader:<\/h3>\n<ul>\n<li>The FileList object returned after a user selects files using the input&gt; element or drag and drop operation&#8217;s DataTransfer property can be used to retrieve file objects.<\/li>\n<li>Only files that have been expressly chosen by the user can be accessed with FileReader, whether you use an HTML input type=\u201cfile&#8221;&gt; element or drag and drop.<\/li>\n<li>From the user&#8217;s file system, it is not possible to read a file by its pathname. Use the File System Access API to get files by pathname on your client&#8217;s file system.<\/li>\n<li>Use the standard Ajax solution with CORS permission to read files on a server if you wish to read cross domains.<\/li>\n<\/ul>\n<h3>Java IO FileReader Class:<\/h3>\n<p>A Java FileReader class that can read a stream of characters out of files is available in the java.io package.<\/p>\n<p>The Java IO FileReading class uses either the requested charset or the default keyboard format on your platform to read from bytes to characters.<\/p>\n<ul>\n<li><strong>Charset:<\/strong> For the purpose of defining methods to make Encoders and Decoders, as well as for retrieving several names in combination with Charsets, a class called Charset is employed.<\/li>\n<li><strong>The default Charset is set as follows:<\/strong> During an implicit computer startup, the default charset is defined, depending on the location and charset of the underlying operating system.<\/li>\n<\/ul>\n<h3>First, here&#8217;s a Blob-like constructor<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">new File(fileParts, fileName, [options])<\/pre>\n<p><strong>fileParts &#8211;<\/strong> is an array of Blob\/BufferSource\/String values.<br \/>\n<strong>fileName \u2013<\/strong> file name string.<br \/>\n<strong>options &#8211;<\/strong> optional object:<br \/>\n<strong>lastModified \u2013<\/strong> timestamp (integer) of the last modification.<\/p>\n<h3>Conclusion<\/h3>\n<p>Java.io.FileReader class provides a convenient way of reading characterally derived data from the file in Java. You can view the contents of a file&#8217;s characters by character when you create an object called FileReader and use your Read() method. Remember to handle potential exceptions by wrapping the code in a try-catch block and closing the file reader object after reading the file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using Files or Blob objects to specify the file or data you want to read, the FileReader object allows Web applications to read the contents of files or raw data buffers stored by a&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447407,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[5605,5606,365,366,5653,296,367,368,5652,263,327,250],"class_list":["post-90028","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-filereader","tag-filereader-class","tag-filereader-class-in-java","tag-filereader-in-java","tag-io-filereader-class","tag-java","tag-java-filereader","tag-java-filereader-class","tag-java-io-fliereader-class","tag-java-tutorial-for-beginners","tag-java-tutorials","tag-learn-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java FileReader Class with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Java.io.FileReader class provides a convenient way of reading characterally derived data from the file in Java.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java FileReader Class with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Java.io.FileReader class provides a convenient way of reading characterally derived data from the file in Java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-06-17T12:30:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-17T12:39:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-filereader.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java FileReader Class with Examples - TechVidvan","description":"Java.io.FileReader class provides a convenient way of reading characterally derived data from the file in Java.","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:\/\/techvidvan.com\/tutorials\/java-filereader-class\/","og_locale":"en_US","og_type":"article","og_title":"Java FileReader Class with Examples - TechVidvan","og_description":"Java.io.FileReader class provides a convenient way of reading characterally derived data from the file in Java.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-06-17T12:30:43+00:00","article_modified_time":"2024-06-17T12:39:00+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-filereader.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java FileReader Class with Examples","datePublished":"2024-06-17T12:30:43+00:00","dateModified":"2024-06-17T12:39:00+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/"},"wordCount":439,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-filereader.webp","keywords":["fileReader","fileReader class","fileReader class in java","fileReader in java","io filereader class","java","java fileReader","java fileReader class","java io fliereader class","java tutorial for beginners","java tutorials","Learn Java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/","url":"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/","name":"Java FileReader Class with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-filereader.webp","datePublished":"2024-06-17T12:30:43+00:00","dateModified":"2024-06-17T12:39:00+00:00","description":"Java.io.FileReader class provides a convenient way of reading characterally derived data from the file in Java.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-filereader.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/09\/java-filereader.webp","width":1200,"height":628,"caption":"java filereader"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-filereader-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java FileReader Class with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/90028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=90028"}],"version-history":[{"count":3,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/90028\/revisions"}],"predecessor-version":[{"id":447513,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/90028\/revisions\/447513"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447407"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=90028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=90028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=90028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}