java stdout_算法java(Robert Sedgewick)基本API-StdOut.java

/*************************************************************************

* Compilation: javac StdOut.java

* Execution: java StdOut

*

* Writes data of various types to standard output.

*

*************************************************************************/

importjava.io.OutputStreamWriter;importjava.io.PrintWriter;importjava.io.UnsupportedEncodingException;importjava.util.Locale;/*** Standard output. This class provides methods for writing strings

* and numbers to standard output.

*

* For additional documentation, see Section 1.5 of

* Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.

*

*@authorRobert Sedgewick

*@authorKevin Wayne*/

public final classStdOut {//force Unicode UTF-8 encoding; otherwise it's system dependent

private static final String CHARSET_NAME = "UTF-8";//assume language = English, country = US for consistency with StdIn

private static final Locale LOCALE =Locale.US;//send output here

private staticPrintWriter out;//this is called before invoking any methods

static{try{

out= new PrintWriter(new OutputStreamWriter(System.out, CHARSET_NAME), true);

}catch(UnsupportedEncodingException e) { System.out.println(e); }

}//don't instantiate

privateStdOut() { }//close the output stream (not required)

/*** Close standard output.*/

public static voidclose() {

out.close();

}/*** Terminate the current line by printing the line separator string.*/

public static voidprintln() {

out.println();

}/*** Print an object to standard output and then terminate the line.*/

public static voidprintln(Object x) {

out.println(x);

}/*** Print a boolean to standard output and then terminate the line.*/

public static void println(booleanx) {

out.println(x);

}/*** Print a char to standard output and then terminate the line.*/

public static void println(charx) {

out.println(x);

}/*** Print a double to standard output and then terminate the line.*/

public static void println(doublex) {

out.println(x);

}/*** Print a float to standard output and then terminate the line.*/

public static void println(floatx) {

out.println(x);

}/*** Print an int to standard output and then terminate the line.*/

public static void println(intx) {

out.println(x);

}/*** Print a long to standard output and then terminate the line.*/

public static void println(longx) {

out.println(x);

}/*** Print a short to standard output and then terminate the line.*/

public static void println(shortx) {

out.println(x);

}/*** Print a byte to standard output and then terminate the line.*/

public static void println(bytex) {

out.println(x);

}/*** Flush standard output.*/

public static voidprint() {

out.flush();

}/*** Print an Object to standard output and flush standard output.*/

public static voidprint(Object x) {

out.print(x);

out.flush();

}/*** Print a boolean to standard output and flush standard output.*/

public static void print(booleanx) {

out.print(x);

out.flush();

}/*** Print a char to standard output and flush standard output.*/

public static void print(charx) {

out.print(x);

out.flush();

}/*** Print a double to standard output and flush standard output.*/

public static void print(doublex) {

out.print(x);

out.flush();

}/*** Print a float to standard output and flush standard output.*/

public static void print(floatx) {

out.print(x);

out.flush();

}/*** Print an int to standard output and flush standard output.*/

public static void print(intx) {

out.print(x);

out.flush();

}/*** Print a long to standard output and flush standard output.*/

public static void print(longx) {

out.print(x);

out.flush();

}/*** Print a short to standard output and flush standard output.*/

public static void print(shortx) {

out.print(x);

out.flush();

}/*** Print a byte to standard output and flush standard output.*/

public static void print(bytex) {

out.print(x);

out.flush();

}/*** Print a formatted string to standard output using the specified

* format string and arguments, and flush standard output.*/

public static voidprintf(String format, Object... args) {

out.printf(LOCALE, format, args);

out.flush();

}/*** Print a formatted string to standard output using the specified

* locale, format string, and arguments, and flush standard output.*/

public static voidprintf(Locale locale, String format, Object... args) {

out.printf(locale, format, args);

out.flush();

}//This method is just here to test the class

public static voidmain(String[] args) {//write to stdout

StdOut.println("Test");

StdOut.println(17);

StdOut.println(true);

StdOut.printf("%.6f\n", 1.0/7.0);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值