1.
1 package algorithms.util; 2 3 /****************************************************************************** 4 * Compilation: javac Out.java 5 * Execution: java Out 6 * Dependencies: none 7 * 8 * Writes data of various types to: stdout, file, or socket. 9 * 10 ******************************************************************************/ 11 12 13 import java.io.FileOutputStream; 14 import java.io.IOException; 15 import java.io.OutputStream; 16 import java.io.OutputStreamWriter; 17 import java.io.PrintWriter; 18 import java.net.Socket; 19 import java.util.Locale; 20 21 /** 22 * This class provides methods for writing strings and numbers to 23 * various output streams, including standard output, file, and sockets. 24 * <p> 25 * For additional documentation, see 26 * <a href="http://introcs.cs.princeton.edu/31datatype">Section 3.1</a> of 27 * <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> 28 * by Robert Sedgewick and Kevin Wayne. 29 * 30 * @author Robert Sedgewick 31 * @author Kevin Wayne 32 */ 33 public class Out { 34 35 // force Unicode UTF-8 encoding; otherwise it‘s system dependent 36 private static final String CHARSET_NAME = "UTF-8"; 37 38 // assume language = English, country = US for consistency with In 39 private static final Locale LOCALE = Locale.US; 40 41 private PrintWriter out; 42 43 /** 44 * Initializes an output stream from a {@link OutputStream}. 45 * 46 * @param os the <tt>OutputStream</tt> 47 */ 48 public Out(OutputStream os) { 49 try { 50 OutputStreamWriter osw = new OutputStreamWriter(os, CHARSET_NAME); 51 out = new PrintWriter(osw, true); 52 } 53 catch (IOException e) { 54 e.printStackTrace(); 55 } 56 } 57 58 /** 59 * Initializes an output stream from standard output. 60 */ 61 public Out() { 62 this(System.out); 63 } 64 65 /** 66 * Initializes an output stream from a socket. 67 * 68 * @param socket the socket 69 */ 70 public Out(Socket socket) { 71 try { 72 OutputStream os = socket.getOutputStream(); 73 OutputStreamWriter osw = new OutputStreamWriter(os, CHARSET_NAME); 74 out = new PrintWriter(osw, true); 75 } 76 catch (IOException e) { 77 e.printStackTrace(); 78 } 79 } 80 81 /** 82 * Initializes an output stream from a file. 83 * 84 * @param filename the name of the file 85 */ 86 public Out(String filename) { 87 try { 88 OutputStream os = new FileOutputStream(filename); 89 OutputStreamWriter osw = new OutputStreamWriter(os, CHARSET_NAME); 90 out = new PrintWriter(osw, true); 91 } 92 catch (IOException e) { 93 e.printStackTrace(); 94 } 95 } 96 97 /** 98 * Closes the output stream. 99 */ 100 public void close() { 101 out.close(); 102 } 103 104 /** 105 * Terminates the current line by printing the line-separator string. 106 */ 107 public void println() { 108 out.println(); 109 } 110 111 /** 112 * Prints an object to this output stream and then terminates the line. 113 * 114 * @param x the object to print 115 */ 116 public void println(Object x) { 117 out.println(x); 118 } 119 120 /** 121 * Prints a boolean to this output stream and then terminates the line. 122 * 123 * @param x the boolean to print 124 */ 125 public void println(boolean x) { 126 out.println(x); 127 } 128 129 /** 130 * Prints a character to this output stream and then terminates the line. 131 * 132 * @param x the character to print 133 */ 134 public void println(char x) { 135 out.println(x); 136 } 137 138 /** 139 * Prints a double to this output stream and then terminates the line. 140 * 141 * @param x the double to print 142 */ 143 public void println(double x) { 144 out.println(x); 145 } 146 147 /** 148 * Prints a float to this output stream and then terminates the line. 149 * 150 * @param x the float to print 151 */ 152 public void println(float x) { 153 out.println(x); 154 } 155 156 /** 157 * Prints an integer to this output stream and then terminates the line. 158 * 159 * @param x the integer to print 160 */ 161 public void println(int x) { 162 out.println(x); 163 } 164 165 /** 166 * Prints a long to this output stream and then terminates the line. 167 * 168 * @param x the long to print 169 */ 170 public void println(long x) { 171 out.println(x); 172 } 173 174 /** 175 * Prints a byte to this output stream and then terminates the line. 176 * <p> 177 * To write binary data, see {@link BinaryOut}. 178 * 179 * @param x the byte to print 180 */ 181 public void println(byte x) { 182 out.println(x); 183 } 184 185 186 187 /** 188 * Flushes this output stream. 189 */ 190 public void print() { 191 out.flush(); 192 } 193 194 /** 195 * Prints an object to this output stream and flushes this output stream. 196 * 197 * @param x the object to print 198 */ 199 public void print(Object x) { 200 out.print(x); 201 out.flush(); 202 } 203 204 /** 205 * Prints a boolean to this output stream and flushes this output stream. 206 * 207 * @param x the boolean to print 208 */ 209 public void print(boolean x) { 210 out.print(x); 211 out.flush(); 212 } 213 214 /** 215 * Prints a character to this output stream and flushes this output stream. 216 * 217 * @param x the character to print 218 */ 219 public void print(char x) { 220 out.print(x); 221 out.flush(); 222 } 223 224 /** 225 * Prints a double to this output stream and flushes this output stream. 226 * 227 * @param x the double to print 228 */ 229 public void print(double x) { 230 out.print(x); 231 out.flush(); 232 } 233 234 /** 235 * Prints a float to this output stream and flushes this output stream. 236 * 237 * @param x the float to print 238 */ 239 public void print(float x) { 240 out.print(x); 241 out.flush(); 242 } 243 244 /** 245 * Prints an integer to this output stream and flushes this output stream. 246 * 247 * @param x the integer to print 248 */ 249 public void print(int x) { 250 out.print(x); 251 out.flush(); 252 } 253 254 /** 255 * Prints a long integer to this output stream and flushes this output stream. 256 * 257 * @param x the long integer to print 258 */ 259 public void print(long x) { 260 out.print(x); 261 out.flush(); 262 } 263 264 /** 265 * Prints a byte to this output stream and flushes this output stream. 266 * 267 * @param x the byte to print 268 */ 269 public void print(byte x) { 270 out.print(x); 271 out.flush(); 272 } 273 274 /** 275 * Prints a formatted string to this output stream, using the specified format 276 * string and arguments, and then flushes this output stream. 277 * 278 * @param format the format string 279 * @param args the arguments accompanying the format string 280 */ 281 public void printf(String format, Object... args) { 282 out.printf(LOCALE, format, args); 283 out.flush(); 284 } 285 286 /** 287 * Prints a formatted string to this output stream, using the specified 288 * locale, format string, and arguments, and then flushes this output stream. 289 * 290 * @param locale the locale 291 * @param format the format string 292 * @param args the arguments accompanying the format string 293 */ 294 public void printf(Locale locale, String format, Object... args) { 295 out.printf(locale, format, args); 296 out.flush(); 297 } 298 299 300 /** 301 * A test client. 302 */ 303 public static void main(String[] args) { 304 Out out; 305 306 // write to stdout 307 out = new Out(); 308 out.println("Test 1"); 309 out.close(); 310 311 // write to a file 312 out = new Out("test.txt"); 313 out.println("Test 2"); 314 out.close(); 315 } 316 317 }
时间: 2024-10-27 17:38:39