23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

16.4 Specifying Width with FieldPosition<br />

<strong>Java</strong> I/O<br />

The <strong>Java</strong> core API does not include any classes that pad numbers with spaces like the<br />

traditional I/O APIs in Fortran, C, and other languages. Part of the reason is that it's no longer<br />

a valid assumption that all output is written in a monospaced font on a VT-100 terminal.<br />

Therefore, spaces are insufficient to line up numbers in tables. Ideally, if you're writing<br />

tabular data in a GUI, you can use a real table component like JTable in the <strong>Java</strong> foundation<br />

classes. If that's not possible, you can measure the width of the string using a FontMetrics<br />

object and offset the position at which you draw the string. And if you are outputting to a<br />

terminal or a monospaced font, then you can manually prefix the string with the right number<br />

of spaces.<br />

The java.text.FieldPosition class separates strings into their component parts, called<br />

fields. (This is another unfortunate example of an overloaded term. These fields have nothing<br />

to do with the fields of a <strong>Java</strong> class.) For example, a typical date string can be separated into<br />

18 fields including era, year, month, day, date, hour, minute, second, and so on. Of course, not<br />

all of these may be present in any given string. For example, 1999 CE includes only a year<br />

and an era field. The different fields that can be parsed are represented as public final<br />

static int fields (there's that annoying overloading again) in the corresponding format class.<br />

The java.text.DateFormat class defines these kinds of fields as mnemonic constants:<br />

public static final int ERA_FIELD<br />

public static final int YEAR_FIELD<br />

public static final int MONTH_FIELD<br />

public static final int DATE_FIELD<br />

public static final int HOUR_OF_DAY1_FIELD<br />

public static final int HOUR_OF_DAY0_FIELD<br />

public static final int MINUTE_FIELD<br />

public static final int SECOND_FIELD<br />

public static final int MILLISECOND_FIELD<br />

public static final int DAY_OF_WEEK_FIELD<br />

public static final int DAY_OF_YEAR_FIELD<br />

public static final int DAY_OF_WEEK_IN_MONTH_FIELD<br />

public static final int WEEK_OF_YEAR_FIELD<br />

public static final int WEEK_OF_MONTH_FIELD<br />

public static final int AM_PM_FIELD<br />

public static final int HOUR1_FIELD<br />

public static final int HOUR0_FIELD<br />

public static final int TIMEZONE_FIELD<br />

Number formats are a little simpler. They are divided into only two fields, the integer field<br />

and the fraction field. These are represented by the mnemonic constants<br />

NumberFormat.INTEGER_FIELD and NumberFormat.FRACT<strong>IO</strong>N_FIELD:<br />

public static final int INTEGER_FIELD<br />

public static final int FRACT<strong>IO</strong>N_FIELD<br />

The integer field is everything before the decimal point. The fraction field is everything after<br />

the decimal point. For instance, the string "-156.32" has an integer field of "-156" and a<br />

fraction field of "32".<br />

The java.text.FieldPosition class identifies the boundaries of each field in the numeric<br />

string. You can then manually add the right number of monospaced characters or pixels to<br />

408

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!