Java. Lang. appendable Interface

Source: Internet
Author: User
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

    1. Appendable append (char c) adds a specified character to this appendable.
    2. Appendable append (charsequence CSQ) adds the specified character sequence to this appendable.
    3. Appendable append (charsequence CSQ, int start, int end) adds a subsequence of the specified character sequence to this appendable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.