001package stdlib; 002/* *********************************************************************** 003 * Compilation: javac Out.java 004 * Execution: java Out 005 * 006 * Writes data of various types to: stdout, file, or socket. 007 * 008 *************************************************************************/ 009 010 011import java.io.FileOutputStream; 012import java.io.IOException; 013import java.io.OutputStream; 014import java.io.OutputStreamWriter; 015import java.io.PrintWriter; 016import java.net.Socket; 017import java.util.Locale; 018 019/** 020 * This class provides methods for writing strings and numbers to 021 * various output streams, including standard output, file, and sockets. 022 * <p> 023 * For additional documentation, see 024 * <a href="http://introcs.cs.princeton.edu/31datatype">Section 3.1</a> of 025 * <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> 026 * by Robert Sedgewick and Kevin Wayne. 027 */ 028public class Out { 029 030 // force Unicode UTF-8 encoding; otherwise it's system dependent 031 private static String charsetName = "UTF-8"; 032 033 // assume language = English, country = US for consistency with In 034 private static final Locale US_LOCALE = new Locale("en", "US"); 035 036 private PrintWriter out; 037 038 /** 039 * Create an Out object using an OutputStream. 040 */ 041 public Out(OutputStream os) { 042 try { 043 OutputStreamWriter osw = new OutputStreamWriter(os, charsetName); 044 out = new PrintWriter(osw, true); 045 } 046 catch (IOException e) { e.printStackTrace(); } 047 } 048 049 /** 050 * Create an Out object using standard output. 051 */ 052 public Out() { this(System.out); } 053 054 /** 055 * Create an Out object using a Socket. 056 */ 057 public Out(Socket socket) { 058 try { 059 OutputStream os = socket.getOutputStream(); 060 OutputStreamWriter osw = new OutputStreamWriter(os, charsetName); 061 out = new PrintWriter(osw, true); 062 } 063 catch (IOException e) { e.printStackTrace(); } 064 } 065 066 /** 067 * Create an Out object using a file specified by the given name. 068 */ 069 public Out(String s) { 070 try { 071 OutputStream os = new FileOutputStream(s); 072 OutputStreamWriter osw = new OutputStreamWriter(os, charsetName); 073 out = new PrintWriter(osw, true); 074 } 075 catch (IOException e) { e.printStackTrace(); } 076 } 077 078 /** 079 * Close the output stream. 080 */ 081 public void close() { out.close(); } 082 083 084 085 /** 086 * Terminate the line. 087 */ 088 public void println() { 089 out.println(); 090 } 091 092 /** 093 * Print an object and then terminate the line. 094 */ 095 public void println(Object x) { 096 out.println(x); 097 } 098 099 /** 100 * Print a boolean and then terminate the line. 101 */ 102 public void println(boolean x) { 103 out.println(x); 104 } 105 106 /** 107 * Print a char and then terminate the line. 108 */ 109 public void println(char x) { 110 out.println(x); 111 } 112 113 /** 114 * Print an double and then terminate the line. 115 */ 116 public void println(double x) { 117 out.println(x); 118 } 119 120 /** 121 * Print a float and then terminate the line. 122 */ 123 public void println(float x) { 124 out.println(x); 125 } 126 127 /** 128 * Print an int and then terminate the line. 129 */ 130 public void println(int x) { 131 out.println(x); 132 } 133 134 /** 135 * Print a long and then terminate the line. 136 */ 137 public void println(long x) { 138 out.println(x); 139 } 140 141 /** 142 * Print a byte and then terminate the line. 143 */ 144 public void println(byte x) { 145 out.println(x); 146 } 147 148 149 150 /** 151 * Flush the output stream. 152 */ 153 public void print() { 154 out.flush(); 155 } 156 157 /** 158 * Print an object and then flush the output stream. 159 */ 160 public void print(Object x) { 161 out.print(x); 162 out.flush(); 163 } 164 165 /** 166 * Print an boolean and then flush the output stream. 167 */ 168 public void print(boolean x) { 169 out.print(x); 170 out.flush(); 171 } 172 173 /** 174 * Print an char and then flush the output stream. 175 */ 176 public void print(char x) { 177 out.print(x); 178 out.flush(); 179 } 180 181 /** 182 * Print an double and then flush the output stream. 183 */ 184 public void print(double x) { 185 out.print(x); 186 out.flush(); 187 } 188 189 /** 190 * Print a float and then flush the output stream. 191 */ 192 public void print(float x) { 193 out.print(x); 194 out.flush(); 195 } 196 197 /** 198 * Print an int and then flush the output stream. 199 */ 200 public void print(int x) { 201 out.print(x); 202 out.flush(); 203 } 204 205 /** 206 * Print a long and then flush the output stream. 207 */ 208 public void print(long x) { 209 out.print(x); 210 out.flush(); 211 } 212 213 /** 214 * Print a byte and then flush the output stream. 215 */ 216 public void print(byte x) { 217 out.print(x); 218 out.flush(); 219 } 220 221 /** 222 * Print a formatted string using the specified format string and arguments, 223 * and then flush the output stream. 224 */ 225 public void format(String format, Object... args) { 226 out.printf(US_LOCALE, format, args); 227 out.flush(); 228 } 229 public void printf(String format, Object... args) { format (format, args); } 230 231 /** 232 * Print a formatted string using the specified locale, format string and arguments, 233 * and then flush the output stream. 234 */ 235 public void format(Locale locale, String format, Object... args) { 236 out.printf(locale, format, args); 237 out.flush(); 238 } 239 public void printf(Locale locale, String format, Object... args) { format (locale, format, args); } 240 241 242 /** 243 * A test client. 244 */ 245 public static void main(String[] args) { 246 Out out; 247 248 // write to stdout 249 out = new Out(); 250 out.println("Test 1"); 251 out.close(); 252 253 // write to a file 254 out = new Out("test.txt"); 255 out.println("Test 2"); 256 out.close(); 257 } 258 259}