Zoo tutorials: [ SQL | Linux | XML ]
ProgZoo: [ Java | C# | VB | C++ | Perl ]
Log in

A Gentle Introduction to
Java Programming

Tutorial: Reading from a text file

 
A program that processes a text file typically has the following stages: A program with this structure will usually be more efficient than one that the entire file in one go.

1. Reading lines


Big

The text file greeks.txt contains a single word on each line.

This program reads the file line by line and prints all the lines with four characters.

Note that the end-of-line character is not included in the count.

  • The BufferedReader represents a text file that can process one line at a time.
  • We need a FileReader to create the BufferedReader.
  • We use the readLine method to obtain a single line of text.
  • When the file has been exhausted the string returned will be null.