{"id":3945,"date":"2017-08-31T05:19:14","date_gmt":"2017-08-31T05:19:14","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=3945"},"modified":"2021-03-21T04:26:48","modified_gmt":"2021-03-21T04:26:48","slug":"reading-a-file-in-java-using-scanner-class","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/reading-a-file-in-java-using-scanner-class\/","title":{"rendered":"Read a file in Java using Scanner class"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>On this page, we will learn how to read a text file using a Scanner class in a Java program. Scanner class is used to read input from the user using the standard console input peripheral device like a keyboard.<\/p>\n<p><strong><a href=\"https:\/\/www.testingdocs.com\/questions\/how-to-get-input-from-keyboard-using-scanner-class\/\">https:\/\/www.testingdocs.com\/questions\/how-to-get-input-from-keyboard-using-scanner-class\/<\/a><\/strong><\/p>\n<p>In this java program, we will use a Scanner class to read from a text file. The text file is a simple file containing integers on each line. The file name is scores.txt<\/p>\n<h3>Java Code<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.io.File;\r\nimport java.io.IOException;\r\nimport java.util.Scanner;\r\n\r\npublic class ReadTextFileUsingScannerDemo {\r\n\r\n  public static void main(String[] args) {\r\n    try\r\n    {   Scanner sc = new Scanner( new File(\"scores.txt\") );\r\n    while( sc.hasNextInt() )\r\n    {\tSystem.out.println( sc.nextInt() );\r\n    }\r\n    }\r\n    catch(IOException e)\r\n    {\tSystem.out.println( e.getMessage() );\r\n    }\r\n\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Sample Output<\/h3>\n<p>90<br \/>\n95<br \/>\n80<br \/>\n85<br \/>\n73<\/p>\n<h3>Screenshot<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3949\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/ReadTextFileUsingScanner.png\" alt=\"ReadTextFileUsingScanner\" width=\"1727\" height=\"913\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/ReadTextFileUsingScanner.png 1727w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/ReadTextFileUsingScanner-300x159.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/ReadTextFileUsingScanner-1024x541.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/ReadTextFileUsingScanner-768x406.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/ReadTextFileUsingScanner-1536x812.png 1536w\" sizes=\"auto, (max-width: 1727px) 100vw, 1727px\" \/><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-3945\" data-postid=\"3945\" class=\"themify_builder_content themify_builder_content-3945 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Introduction On this page, we will learn how to read a text file using a Scanner class in a Java program. Scanner class is used to read input from the user using the standard console input peripheral device like a keyboard. https:\/\/www.testingdocs.com\/questions\/how-to-get-input-from-keyboard-using-scanner-class\/ In this java program, we will use a Scanner class to read from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-3945","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=3945"}],"version-history":[{"count":4,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3945\/revisions"}],"predecessor-version":[{"id":19695,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/3945\/revisions\/19695"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=3945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=3945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=3945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}