The object of the implementation class of the appendable interface can be added with the char sequence and value. If an instance of a class wants to receive formatted output from Java. util. formatter, the class must implement the appendable interface.
The characters to be added must be valid Unicode characters.
Appendable does not need to be secure for multi-threaded access. Thread security is the responsibility of the class that extends and implements this interface.
All known implementation classes:
Bufferedwriter, chararraywriter, charbuffer, filewriter, filterwriter, logstream, outputstreamwriter, pipedwriter, printstream, printwriter, stringbuffer, stringbuilder, stringwriter, writer
Example of the expected usage:
Stringbuilder sb = new stringbuilder (); // stringbuilder implements the appendable Interface
// Send all output to the appendable object sb
Formatter = new formatter (SB, locale. US );
// Explicit argument indices may be used to re-order output.
// Put the formatted string into sb
Formatter. format ("% 4 $2 S % 3 $2 S % 2 $2 S % 1 $2 s", "A", "B", "C ", "D ")
//-> "D c B"
// Optional locale as the first argument can be used to get
// Locale-specific formatting of numbers. The precision and width can be
// Given to round and align the value.
Formatter. Format (locale. France, "E = % + 10.4f", math. E );
//-> "E = + 2,7183"
// The '('numeric flag may be used to format negative numbers
// Parentheses rather than a minus sign. Group separators are
// Automatically inserted.
Formatter. Format ("amount gained or lost since last statement: $ % (,. 2f", balancedelta );
//-> "Amount gained or lost since last statement: $(6,217.58 )"
Common convenient methods for formatting requests are described in the following call format:
// Writes a formatted string to system. Out.
System. Out. Format ("local time: % TT", calendar. getinstance ());
//-> "Local time: 13:34:18"
// Writes formatted output to system. Err.
System. Err. printf ("unable to open file '% 1 $ s': % 2 $ s ",
Filename, exception. getmessage ());
//-> "Unable to open file 'food': no such file or directory"
Similar to sprintf (3) in C, you can use the static method string. Format (string, object...) to format strings:
// Format a string containing a date.
Import java. util. calendar;
Import java. util. gregoriancalendar;
Import static java. util. Calendar .*;
Calendar c = new gregoriancalendar (1995, May, 23 );
String S = string. Format ("Duke's birthday: % 1 $ TM % 1 $ te, % 1 $ ty", C );
//-> S = "Duke's birthday: May 23,199 5"
Java. Lang. appendable Interface Method
- Appendable append (char c) adds a specified character to this appendable.
- Appendable append (charsequence CSQ) adds the specified character sequence to this appendable.
- Appendable append (charsequence CSQ, int start, int end) adds a subsequence of the specified character sequence to this appendable.