Home : Course Map : Chapter 9 : Java :
Text Input from a File
JavaTech
Course Map
Chapter 9

Introduction
Overview
Streams
Wrappers,Buffers
Console I/O
  Text Output 
     Demo 1

  Formatter/printf()
     Demo 2

  Tex 2t Input
     Demo 3

  Scanner
     
Demo 4
File Class
  File I/O
  File Output-Text
     Demo 5

  Formatter to File
     Demo 6

  File Input - Text
    Demo 7

  Scanner - Files
     Demo 8

  File I/O - Binary
     Demo 9
   Demo 10
File Chooser Dialog
  Demo 11

Character Codes
  Demo 12
   Demo13
Object I/O
Types to Bytes
Stream Filters
Other I/O Topics
Exercises

    Supplements
Character I/O
  Demo 1   Demo 2
Random Access
  Demo 3
ZIP/GZIP Streams
  Demo 4
Piped Streams
  Demo 5
NIO Framework
More NIO
  Demo 6

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The following example, TextFileReadApp, illustrates how to use the FileReader stream to read strings from a text file. The goal is to read a file and count the number of lines in which a particular string occurs at least once.

We wrap the FileReader stream with a BufferedReader class and take advantage of its readLine() method to read a whole line at a time. We use the indexOf() method in the String class to search for the string of interest.

As usual, we enclose the stream reading within a try-catch statement to catch the IOException or one of its subclass exceptions that can be thrown by the stream constructors and the read methods.

import java.io.*;
import java.util.*;

/** Demonstrate reading text from a file.**/
public class TextFileReadApp
{
  public static void main (String arg[]) {
    // Count the number of lines in which the string occurs
    String string_to_find = "new";

    File file = null;
    // Get the file from the argument line.
    if (arg.length > 0) file = new File (arg[0]);
    if (file == null || !file.exists ()) {
      System.out.println ("Default: TextFileReadApp.java");
      file = new File ("TextFileReadApp.java");
    }

    // Count the number of lines with the string of interest.
    int num_lines = 0;
    try {
      // Create a FileReader and then wrap it with BufferedReader.
      FileReader file_reader = new FileReader (file);
      BufferedReader buf_reader = new BufferedReader (file_reader);

      // Read each line of the file and look for the string of interest.
      do {
         String line = buf_reader.readLine ();
         if (line == null) break;
         if (line.indexOf(string_to_find) != -1) num_lines++;
      } while (true);
      buf_reader.close ();
    }
    catch (IOException e) {
        System.out.println ("IO exception =" + e );
    }
    System.out.printf ("Number of lines containing \"%s\"  = %3d %n",
                         string_to_find, num_lines);
  } // main

} //class TextFileReadApp

If we let the program examine the default input file, then the output will go as

Default: TextFileReadApp.java
Number of lines containing "new" = 5

Note that we close the stream explicitly. For this short program, which stops soon after finishing the read, closing the input stream is not a big deal, but in general, it is good practice to always close input and output streams when I/O transfers are completed.

References & Web Resources

 

Latest update: Dec. 9, 2004

              Tech
Histogram I/O
Hist I/O - Get/Set
  Demo 1
Hist I/O - Objects
  Demo 2
HistogramStream
  Demo 3
Filtering Data
  Demo 4
Exercises

           Physics
Physics Model
Simulation Design
Physics Simulator
  Demo 1
Experiment Design
Experiment Sim.
  Demo 2
Analysis
Expt. + Analysis
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.