Java Reference
In-Depth Information
boolean a - boolean = false;
byte a - byte = 114;
short a - short = 1211;
int an - int = 1234567;
long a - long = 987654321;
float a - float = 983.6f;
double a - double = -4.297e-15;
formatter.format ("boolean = %9b %n", - boolean);
formatter.format ("byte = %9d %n", - byte);
formatter.format ("short = %9d %n", - short);
formatter.format ("int = %9d %n", - int);
formatter.format ("long = %9d %n", - long);
formatter.format ("float = %9.3f %n",a - float);
formatter.format ("double = %9.2e %n",a - double);
// Need to flush the data out of the buffer.
formatter.flush ();
formatter.close ();
} // main
} // class FormatWriteFileApp
This program creates a file that contains the same output as FormatWriteApp
in Section 9.4.2.
9.6.3 Text input from a file
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. **/
Search WWH ::




Custom Search