Java.util - SPLessons

Java.util Formatter

Home > Lesson > Chapter 8
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.util Formatter

Java.util Formatter

shape Description

The java.util Formatter class offers support to outline diversion and plan, essential configurations for string, numeric. It changes parallel data into intelligible forms. The created Output will go to a buffer. Following is the syntax declaration for java.util.Formatter class. [java] public final class Formatter extends Object implements Closeable, Flushable [/java] A Closeable interface is a source/destination of information that can be shut. The flush() is the method Flushable interface.

Constructors Of java.util Formatter Class

shape Constructors

Following are the common using constructors of java.util Formatter Class.
Constructors Description
Formatter() To construct new formatter.
Formatter(File file) To construct new formatter with particular file.
Formatter(Locale l) To construct new formatter with particular locale.
Formatter(PrintStream ps) To construct new formatter with particular print stream.

Methods Of java.util Formatter Class

shape Methods

Java.util Formatter - Following are the common using methods of java.util Formatter Class.
Methods Description
close() To close the formatter.
void flush() To flush the formatter.
Locale locale() To give back the locale set by the constructor.
Appendable out() Destination of an output will be returned.
String toString() To return the result of invoking toString().

By Using java.util.Formatter.flush()

shape Description

Following is the syntax declaration of the method. [java]public void flush()[/java] DemoFormatter.java [java] package com.SPlessons; import java.util.*; public class DemoFormatter { public static void main(String[] args) { // create a new formatter StringBuffer buffer = new StringBuffer(); Formatter formatter = new Formatter(buffer, Locale.US); // format a new string String name = "SPLessons...!"; formatter.format("Welcome To %s !", name); // print the formatted string System.out.println("" + formatter); // flush the formatter. Here it does nothing. formatter.flush(); System.out.println("Formatter Flushed."); } } [/java] Following is the interactive diagram for an above code. Output: Now compile the code result will be as follows. [java] Welcome To SPLessons...! ! Formatter Flushed. [/java]

By Using java.util.Formatter.close()

shape Description

Following is the syntax declaration of the method. [java]public void close()[/java] DemoFormatter.java [java] package com.SPlessons; import java.util.*; public class DemoFormatter { public static void main(String[] args) { // create a new formatter StringBuffer buffer = new StringBuffer(); Formatter formatter = new Formatter(buffer, Locale.US); // format a new string String name = "SPLessons"; formatter.format("Welcome To %s !", name); // print the formatted string System.out.println("" + formatter); // close the formatter formatter.close(); // attempt to access the formatter results in exception System.out.println("" + formatter); } } [/java] The purpose of StringBuffer class is to create mutable string, it can be changed. Output: Now compile the code result will be as follows. [java] Welcome To SPLessons ! Exception in thread "main" java.util.FormatterClosedException at java.util.Formatter.ensureOpen(Unknown Source) at java.util.Formatter.toString(Unknown Source) at java.lang.String.valueOf(Unknown Source) at java.lang.StringBuilder.append(Unknown Source) at com.SPlessons.DemoFormatter.main(DemoFormatter.java:21) [/java]

By Using java.util.Formatter.locale()

shape Description

Following is the syntax declaration of the method. [java]public Locale locale()[/java] DemoFormatter.java [java] package com.SPlessons; import java.util.*; public class DemoFormatter { public static void main(String[] args) { // create a new formatter StringBuffer buffer = new StringBuffer(); Formatter formatter = new Formatter(buffer, Locale.US); // format a new string String name = "To SPLessons"; formatter.format("Welcome %s !", name); // print the formatted string with default locale System.out.println("" + formatter); // print locale System.out.println("" + formatter.locale()); } } [/java] In the above code, the locale of the US has been placed. Output: Now compile the code result will be as follows. [java] Welcome To SPLessons ! en_US [/java]

By Using java.util.Formatter.out()

shape Description

Following is the syntax declaration of the method. [java]public Appendable out()[/java] DemoFormatter.java [java] package com.SPlessons; import java.util.*; public class DemoFormatter { public static void main(String[] args) { // create a new formatter StringBuffer buffer = new StringBuffer(); Formatter formatter = new Formatter(buffer, Locale.US); // format a new string String name = "To SPLessons"; formatter.format("Welcome %s !", name); // print the formatted string with default locale System.out.println("" + formatter); // print the output System.out.println("" + formatter.out()); } } [/java] In the above example, default locale message is going to be printed and out() message will also print. Output: Now compile the code result will be as follows. [java] Welcome To SPLessons ! Welcome To SPLessons ! [/java]

By Using java.util.Formatter.toString()

shape Description

Following is the syntax declaration of the method. [java]public String toString()[/java] DemoFormatter.java [java] package com.SPlessons; import java.util.*; public class DemoFormatter { public static void main(String[] args) { // create a new formatter StringBuffer buffer = new StringBuffer(); Formatter formatter = new Formatter(buffer, Locale.US); // format a new string String name = "SPLessons"; formatter.format("Welcome To %s !", name); // print the formatted string with default locale System.out.println("" + formatter); // print the formatter as a string System.out.println("" + formatter.toString()); } } [/java] Output: Now compile the code result will be as follows. [java] Welcome To SPLessons ! Welcome To SPLessons ! [/java]

Summary

shape Key Points

  • Java.util Formatter - The java.util.Formatter.ioException() is utilized to return IOException.
  • Java.util Formatter - The java.util.Formatter class will hire the methods from java.util.Object class.
  • Java.util Formatter - The Formatter(Appendable a) is the field of java.util.Formatter class to build a new formatter with destination.