9.4 Using the printf( ) Convenience Method
For those of you who are die-hard
C and C++ fans, Tiger gives you the
ability to type printf( ) once more.
9.4.1 How do I do that?
In "Using the format( ) Convenience Method," you saw how both the
PrintStream and PrintWriter classes offer a new method called
format( ) to handle formatted output. Each class also has a method
called printf( ), which does exactly the same thing. That's right
printf( ) and format( ) are interchangeable. So, if you favor using
printf( ) over format( ), you're free to do so, as Example 9-1 shows.
Example 9-1. Using printf( ) instead of format( )
package com.oreilly.tiger.ch09;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class FormatTester {
public static void main(String[] args) {
String filename = args[0];
try {
File file = new File(filename);
FileReader fileReader = new FileReader(file);
BufferedReader reader = new BufferedReader(fileReader);
String line;
int i = 1;
while ((line = reader.readLine( )) != null) {
System.out.printf("Line %d: %s%n", i++, line);
}
} catch (Exception e) {
System.err.printf("Unable to open file named '%s': %s",
filename, e.getMessage( ));
}
}
}