/************************************************************************* * 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;/*** <i>standard output</i>. This class provides methods for writing strings * and numbers to standard output. * <p> * For additional documentation, see <a href= "Http://introcs.cs.princeton.edu/15inout">section 1.5</a> of * <i>introduction to programming in Java:an interdisciplinary Approach</i> 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 FinalString charset_name = "UTF-8"; //Assume language = 中文版, country = US for consistency with StdIn Private Static FinalLocale locale =locale.us; //Send output here Private StaticPrintWriter out; //This was called before invoking any methods Static { Try{ out=NewPrintWriter (NewOutputStreamWriter (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 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 voidprintlnBooleanx) {out.println (x); } /*** Print a char to standard output and then terminate the line. */ Public Static voidprintlnCharx) {out.println (x); } /*** Print a double to standard output and then terminate the line. */ Public Static voidprintlnDoublex) {out.println (x); } /*** Print a float to standard output and then terminate the line. */ Public Static voidprintlnfloatx) {out.println (x); } /*** Print an int to standard output and then terminate the line . */ Public Static voidprintlnintx) {out.println (x); } /*** Print a long to standard output and then terminate the line. */ Public Static voidprintlnLongx) {out.println (x); } /*** Print a short to standard output and then terminate the line . */ Public Static voidprintln Shortx) {out.println (x); } /*** Print a byte to standard output and then terminate the line. */ Public Static voidprintlnbytex) {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 voidPrintBooleanx) {out.print (x); Out.flush (); } /*** Print a char to standard output and flush standard output. */ Public Static voidPrintCharx) {out.print (x); Out.flush (); } /*** Print a double to standard output and flush standard output. */ Public Static voidPrintDoublex) {out.print (x); Out.flush (); } /*** Print a float to standard output and flush standard output. */ Public Static voidPrintfloatx) {out.print (x); Out.flush (); } /*** Print an int to standard output and flush standard output. */ Public Static voidPrintintx) {out.print (x); Out.flush (); } /*** Print a long to standard output and flush standard output. */ Public Static voidPrintLongx) {out.print (x); Out.flush (); } /*** Print a short to standard output and flush standard output. */ Public Static voidPrint Shortx) {out.print (x); Out.flush (); } /*** Print a byte to standard output and flush standard output. */ Public Static voidPrintbytex) {out.print (x); Out.flush (); } /*** Print A formatted string to standard output using the specified * format string and arguments, and flush St Andard 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 was just here to test the class Public Static voidMain (string[] args) {//write to stdoutStdout.println ("Test"); Stdout.println (17); Stdout.println (true); stdout.printf ("%.6f\n", 1.0/7.0); }}
Algorithm Java (Robert Sedgewick) Basic Api-stdout.java