Java Reference
In-Depth Information
LISTING 15.8
Continued
32: BufferedWriter out = new
33: BufferedWriter(fw);
34:
35: boolean eof = false;
36: int inChar = 0;
37: do {
38: inChar = in.read();
39: if (inChar != -1) {
40: char outChar = Character.toUpperCase( (char)inChar );
41: out.write(outChar);
42: } else
43: eof = true;
44: } while (!eof);
45: in.close();
46: out.close();
47:
48: boolean deleted = source.delete();
49: if (deleted)
50: temp.renameTo(source);
51: } catch (IOException e) {
52: System.out.println(“Error — “ + e.toString());
53: } catch (SecurityException se) {
54: System.out.println(“Error — “ + se.toString());
55: }
56: }
57: }
15
After you compile the program, you need a text file that can be converted to all capital
letters. One option is to make a copy of AllCapsDemo.java and give it a name like
TempFile.java .
The name of the file to convert is specified at the command line when running
AllCapsDemo , as in the following JDK example:
java AllCapsDemo TempFile.java
This program does not produce any output. Load the converted file into a text editor to
see the result of the application.
Summary
Today, you learned how to work with streams in two directions: pulling data into a pro-
gram over an input stream and sending data from a program using an output stream.
 
 
Search WWH ::




Custom Search