001package stdlib; 002/* *********************************************************************** 003 * Compilation: javac StdOut.java 004 * Execution: java StdOut 005 * 006 * Writes data of various types to standard output. 007 * 008 *************************************************************************/ 009 010import java.io.FileNotFoundException; 011import java.io.FileOutputStream; 012import java.io.OutputStreamWriter; 013import java.io.PrintWriter; 014import java.io.UnsupportedEncodingException; 015import java.util.Locale; 016 017/** 018 * <i>Standard output</i>. This class provides methods for writing strings 019 * and numbers to standard output. 020 * <p> 021 * For additional documentation, see <a href="http://introcs.cs.princeton.edu/15inout">Section 1.5</a> of 022 * <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> by Robert Sedgewick and Kevin Wayne. 023 */ 024public final class StdOut { 025 026 // force Unicode UTF-8 encoding; otherwise it's system dependent 027 private static final String charsetName = "UTF-8"; 028 029 // assume language = English, country = US for consistency with StdIn 030 private static final Locale US_LOCALE = new Locale("en", "US"); 031 032 // send output here 033 private static PrintWriter out; 034 035 // this is called before invoking any methods 036 static { 037 try { 038 out = new PrintWriter(new OutputStreamWriter(System.out, charsetName), true); 039 } 040 catch (UnsupportedEncodingException e) { System.out.println(e); } 041 } 042 043 // don't instantiate 044 private StdOut() { } 045 046 // close the output stream (not required) 047 /** 048 * Close standard output. 049 */ 050 public static void close() { 051 if (doNothing) return; 052 out.close(); 053 } 054 055 /** 056 * Terminate the current line by printing the line separator string. 057 */ 058 public static void println() { 059 if (doNothing) return; 060 out.println(); 061 } 062 063 /** 064 * Print an object to standard output and then terminate the line. 065 */ 066 public static void println(Object x) { 067 if (doNothing) return; 068 out.println(x); 069 } 070 071 /** 072 * Print a boolean to standard output and then terminate the line. 073 */ 074 public static void println(boolean x) { 075 if (doNothing) return; 076 out.println(x); 077 } 078 079 /** 080 * Print a char to standard output and then terminate the line. 081 */ 082 public static void println(char x) { 083 if (doNothing) return; 084 out.println(x); 085 } 086 087 /** 088 * Print a double to standard output and then terminate the line. 089 */ 090 public static void println(double x) { 091 if (doNothing) return; 092 out.println(x); 093 } 094 095 /** 096 * Print a float to standard output and then terminate the line. 097 */ 098 public static void println(float x) { 099 if (doNothing) return; 100 out.println(x); 101 } 102 103 /** 104 * Print an int to standard output and then terminate the line. 105 */ 106 public static void println(int x) { 107 if (doNothing) return; 108 out.println(x); 109 } 110 111 /** 112 * Print a long to standard output and then terminate the line. 113 */ 114 public static void println(long x) { 115 if (doNothing) return; 116 out.println(x); 117 } 118 119 /** 120 * Print a short to standard output and then terminate the line. 121 */ 122 public static void println(short x) { 123 if (doNothing) return; 124 out.println(x); 125 } 126 127 /** 128 * Print a byte to standard output and then terminate the line. 129 */ 130 public static void println(byte x) { 131 if (doNothing) return; 132 out.println(x); 133 } 134 135 /** 136 * Flush standard output. 137 */ 138 public static void print() { 139 if (doNothing) return; 140 out.flush(); 141 } 142 143 /** 144 * Print an Object to standard output and flush standard output. 145 */ 146 public static void print(Object x) { 147 if (doNothing) return; 148 out.print(x); 149 out.flush(); 150 } 151 152 /** 153 * Print a boolean to standard output and flush standard output. 154 */ 155 public static void print(boolean x) { 156 if (doNothing) return; 157 out.print(x); 158 out.flush(); 159 } 160 161 /** 162 * Print a char to standard output and flush standard output. 163 */ 164 public static void print(char x) { 165 if (doNothing) return; 166 out.print(x); 167 out.flush(); 168 } 169 170 /** 171 * Print a double to standard output and flush standard output. 172 */ 173 public static void print(double x) { 174 if (doNothing) return; 175 out.print(x); 176 out.flush(); 177 } 178 179 /** 180 * Print a float to standard output and flush standard output. 181 */ 182 public static void print(float x) { 183 if (doNothing) return; 184 out.print(x); 185 out.flush(); 186 } 187 188 /** 189 * Print an int to standard output and flush standard output. 190 */ 191 public static void print(int x) { 192 if (doNothing) return; 193 out.print(x); 194 out.flush(); 195 } 196 197 /** 198 * Print a long to standard output and flush standard output. 199 */ 200 public static void print(long x) { 201 if (doNothing) return; 202 out.print(x); 203 out.flush(); 204 } 205 206 /** 207 * Print a short to standard output and flush standard output. 208 */ 209 public static void print(short x) { 210 if (doNothing) return; 211 out.print(x); 212 out.flush(); 213 } 214 215 /** 216 * Print a byte to standard output and flush standard output. 217 */ 218 public static void print(byte x) { 219 if (doNothing) return; 220 out.print(x); 221 out.flush(); 222 } 223 224 /** 225 * Print a formatted string to standard output using the specified 226 * format string and arguments, and flush standard output. 227 */ 228 public static void format(String format, Object... args) { 229 if (doNothing) return; 230 out.printf(US_LOCALE, format, args); 231 out.flush(); 232 } 233 public static void printf(String format, Object... args) { format (format, args); } 234 235 /** 236 * Print a formatted string to standard output using the specified 237 * locale, format string, and arguments, and flush standard output. 238 */ 239 public static void format(Locale locale, String format, Object... args) { 240 if (doNothing) return; 241 out.printf(locale, format, args); 242 out.flush(); 243 } 244 public static void printf(Locale locale, String format, Object... args) { format (locale, format, args); } 245 246 247 /** 248 * Redirect to a file. This is a hack to get programs to work easily in eclipse. 249 * (Added by James Riely 2012/01/12.) 250 */ 251 public static void toFile(String filename) { 252 if (doNothing) return; 253 try { 254 StdOut.out = new PrintWriter(new OutputStreamWriter(new FileOutputStream (filename), charsetName), true); 255 } catch (FileNotFoundException e) { 256 throw new Error (e.getMessage ()); 257 } catch (UnsupportedEncodingException e) { 258 throw new Error (e.getMessage ()); 259 } 260 } 261 262 /** 263 * Makes it so that this class does nothing; handy for performance testing 264 * (Added by James Riely 2014/02/23.) 265 */ 266 public static void doNothing () { doNothing = true; } 267 private static boolean doNothing = false; 268 269 // This method is just here to test the class 270 public static void main(String[] args) { 271 272 // write to stdout 273 StdOut.println("Test"); 274 StdOut.println(17); 275 StdOut.println(true); 276 StdOut.format("%.6f\n", 1.0/7.0); 277 } 278 279}