Simple Input Example: Counting Characters in a File
/**
 * A program to count the total number of characters in a file with a
 * separate count of the number of white space characters.  The file name
 * is taken from the command line.  If no file name is given, then 
 * the standard input stream, System.in, is used as the input.
 */

import java.io.*;	// For InputStream, FileInputStream, and read()


class CharCount{

    public static void main( String[] arg )
        throws IOException
    {
        InputStream in;			// Declare var of type InputStream
        if ( arg.length == 0 )		// Check for command line param
            in = System.in;		// Default input stream
        else
            in = new FileInputStream( arg[0] );	// Open command line file

        int ch;
        int charCt;			// Count of all characters
        int spaces = 0;			// Count of whitespace chars

        for ( charCt = 0; (ch = in.read()) != -1; charCt++ ){
            if ( Character.isWhitespace( (char)ch ) )
                spaces++;
        }

        System.out.println( "Read " + charCt + " total characters and "
                            + spaces + " spaces." );
    }
}
The program produces the following output when applied to its own java source file:
Read 1120 total characters and 366 spaces.
Link to the source code.