Formatted I/O with java.text

Formatted I O with java text

Java 1.4 and earlier have no equivalent of printf( ). Even Java 6 has no equivalent of scanf( ) . Part of the reason is that Java didn support the variable-length argument lists on which these functions depend until Java 5. However, the real reason Java didn have equivalents to Cs formatted I/O routines is a difference in philosophy. Cs printf( ), scanf( ), and related functions combine number formatting and parsing with I/O in an inflexible manner. Java separates number formatting and I/O into separate packages and by so doing produces a much more general and powerful system.

Of course, starting in version 5, Java does have variable-length argument lists and printf( ), as you saw in Chapter 7 (though scanf( ) is still missing). The printf( ) functionality introduced in Java 5 is really just a thin layer on top of the classes discussed in this chapter. To be honest, Im not convinced this is an improvement.


More than one programmer has attempted to recreate printf( ) and scanf( ) in Java. However, overloading the + signs for string concatenation is easily as effective, probably more so, since it doesn share the problems of mismatched argument lists. For example, which is clearer to you? This:

System.out.printf("%s worked %d hours at $%d per/hour for a total of %d dollars.
",
 hours, salary, hours*salary);

or this:

System.out.println(employee + " worked " + hours + " hours at $" + salary
 + "per/hour for a total of $" + hours*salary);

Id argue that the second is clearer. Among other advantages, it avoids problems with mismatched format strings and argument lists. (Did you notice that an argument is missing from the previous printf( ) statement?) On the flip side, the format string approach is a little less prone to missing spaces. (Did you notice that the println( ) statement would print pay scales as "$5.35per/hour" rather than "$5.35 per/hour"?) However, this is only a cosmetic problem and is easily fixed.

The real advantage of the printf( )/scanf( ) family of functions is not the format string. Its number formatting:

printf(
"%s worked %4.1d hours at $%6.2d per/hour for a total of %8.2d dollars.
",
 employee, hours, salary, hours*salary);

Javas been able to format numbers like this since version 1.1. However, its done so with the java.text.NumberFormat class rather than with embedded control codes in format strings.


Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O

Buffers

Channels

Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers

Text

Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text

Devices

The Java Communications API

USB

The J2ME Generic Connection Framework

Bluetooth

Character Sets



Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net