Read from a file using a BufferedReader



For reading text from a file it’s better to use a Reader class instead of a InputStream class since Reader classes have the purpose of reading textual input.
In this example we use the BufferedReader class to read text from a file line by line using it’s readLine method.
It is possible to read the data the same way as with the BufferedInputStream using either a byte array or read one byte at a time, but if you’re going to read plain text this is easier and produce more readable code.

In the example we simply create a BufferedReader object and start reading from the file line by line until the readLine method returns null, which indicates the end of the file.

See this example if you want to compare with reading from a BufferedInputStream:

Read from file with BufferedInputStream







Search for more Java info on this site here:
Custom Search


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">