{"id":335,"date":"2013-11-20T16:26:33","date_gmt":"2013-11-20T05:26:33","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=335"},"modified":"2015-11-20T17:01:16","modified_gmt":"2015-11-20T06:01:16","slug":"read-text-file-line-line","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/read-text-file-line-line\/","title":{"rendered":"How do I read a text file line by line?"},"content":{"rendered":"

The readLine() method of java.io.BufferedReader class reads the next line from a text file. When it reaches the end of the file it will return null.<\/p>\n

\r\nBufferedReader in = new BufferedReader(new FileReader(filename));\r\nString line = null;\r\nwhile (null!=(line=in.readLine()))\r\n{\r\n   \/\/ process each line of file her\r\n   \/\/ 'line' variable contains next line in file\r\n}\r\nin.close();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

The readLine() method of java.io.BufferedReader class reads the next line from a text file. When it reaches the end of the file it will return null.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4,89],"tags":[94,76,95,97,96],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-5p","jetpack-related-posts":[{"id":339,"url":"https:\/\/learn-java-by-example.com\/java\/scanner-read-words-text-file\/","url_meta":{"origin":335,"position":0},"title":"Using Scanner to read words from text file","date":"November 20, 2014","format":false,"excerpt":"Often you need to read a file line by line. Alternatively sometimes you want to read text word by word (for example to count the occurrence of different words). The Scanner classes next() method can be used for this as shown in the following example. You can find an example\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":205,"url":"https:\/\/learn-java-by-example.com\/java\/counting-numbers\/","url_meta":{"origin":335,"position":1},"title":"Counting numbers","date":"September 12, 2011","format":false,"excerpt":"The assignment here is to read a text file containing a list of numbers. We need to report how many numbers were in the file, the sum of all the numbers, the average of all numbers, and the minimum and maximum number. First step is to read the file and\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":253,"url":"https:\/\/learn-java-by-example.com\/java\/copy-file-java-6\/","url_meta":{"origin":335,"position":2},"title":"How to copy a file using Java 6","date":"July 10, 2015","format":false,"excerpt":"Prior to Java 7, Java did not provide a standard method to copy a file. To implement copying you needed to read all bytes from source file and write to destination. The read() method will return -1 when eof is reached, otherwise it returns the number of bytes read.","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":98,"url":"https:\/\/learn-java-by-example.com\/java\/calculator-keypad\/","url_meta":{"origin":335,"position":3},"title":"Calculator Keypad","date":"August 14, 2010","format":false,"excerpt":"This problem involves understanding some of the basics of building a Swing application including how individual components are laid out inside a window, and how your application can react to the user interacting with the application. In this case we need to update a text field whenever a button is\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":375,"url":"https:\/\/learn-java-by-example.com\/java\/prompt-user-console-input-validate-response\/","url_meta":{"origin":335,"position":4},"title":"Prompt user for console input and validate their response","date":"December 6, 2015","format":false,"excerpt":"We often get students struggling to read and validate console input from the user. The following example gives a simple example of an approach that can be used. It reads the input from the user and checks if it is a valid double. If its not it prompts the user\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":96,"url":"https:\/\/learn-java-by-example.com\/java\/simple-mortgage-calculator\/","url_meta":{"origin":335,"position":5},"title":"Simple Mortgage Calculator","date":"June 5, 2010","format":false,"excerpt":"This example expands on the Monthly Payment Calculator example we posted earlier. As well as calculating the monthly payment for a loan, it then goes on to use that to calculate the balance of the loan after each payment. This is often referred to as an amortisation schedule. The displayMonthlyBalance()\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/335"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":2,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"predecessor-version":[{"id":352,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/335\/revisions\/352"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}