Menu
Topics Index
...
`


More Utility Classes > Formatter >
Siva Nookala - 15 Apr 2016
Formatter class in java.util package helps in formatting the display numbers, string, time and date in any format you like. For e.g., we could write the amount 123456.7 as 1,23,456.7 or 123,456.7 or 123456.70 or 123.456,70 depending upon the requirement. The Formatter class helps in displaying them in required format.

Initially we will discuss various ways for creating the Formatter objects. Some of the constructors are
/*1*/ Formatter()
/*2*/ Formatter(Appendable buf)
/*3*/ Formatter(Appendable buf, Locale loc)
/*4*/ Formatter(String filename) throws FileNotFoundException
/*5*/ Formatter(String filename, String charset) throws FileNotFoundException, UnsupportedEncodingException
/*6*/ Formatter(File outF) throws FileNotFoundException
/*7*/ Formatter(OutputStream outStream)
The most commonly used constructor is the first one and by default any data formatted using this type of Formatter, will be written to StringBuilder and using the default Locale. The output of the Formatter can be accessed using out() method.
If we want to the output to be written to our own StringBuilder then we can use the constructor 2 and 3. If we want to write to file or OutputStream we can use the other constructors. We can also pass the Locale and charset as required.

In Java Formatter Methods we will discuss about various methods in the Formatter class.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App