Nov 102012
Article Java

The following java sample code is probably the easiest way to write a text file:

Writting formatted data

The “print()” and “println” ¬†methods convert automatically integer and floating point data into their string representations.

We can write:

and the resulting output is:

As we can see, println() converts the integer number into the base 10 string representacion. It also writes the floating point number using 16 decimal positions.

If we want more control over the formatting of the data, we can use the format() method. This method works like the “printf” function in the C language: it receives as arguments a format specification in a text string, followed by the values that have to be substituted in that specification. For instance, the sentences:

produce the output:

The reference documentation can be consulted in the Oracle web site: format specifiers reference documentation.

 Posted by at 9:02 pm

 Leave a Reply