本章介紹PrintStream以及 它與DataOutputStream的區別。我們先對PrintStream有個大致認識,然后再深入學習它的源碼,最后通過示例加深對它的了解。
轉載請注明出處:http://www.cnblogs.com/skywang12345/p/io_16.html
PrintStream 介紹
PrintStream 是打印輸出流,它繼承於FilterOutputStream。
PrintStream 是用來裝飾其它輸出流。它能為其他輸出流添加了功能,使它們能夠方便地打印各種數據值表示形式。
與其他輸出流不同,PrintStream 永遠不會拋出 IOException;它產生的IOException會被自身的函數所捕獲並設置錯誤標記, 用戶可以通過 checkError() 返回錯誤標記,從而查看PrintStream內部是否產生了IOException。
另外,PrintStream 提供了自動flush 和 字符集設置功能。所謂自動flush,就是往PrintStream寫入的數據會立刻調用flush()函數。
PrintStream 函數列表
/* * 構造函數 */ // 將“輸出流out”作為PrintStream的輸出流,不會自動flush,並且采用默認字符集 // 所謂“自動flush”,就是每次執行print(), println(), write()函數,都會調用flush()函數; // 而“不自動flush”,則需要我們手動調用flush()接口。 PrintStream(OutputStream out) // 將“輸出流out”作為PrintStream的輸出流,自動flush,並且采用默認字符集。 PrintStream(OutputStream out, boolean autoFlush) // 將“輸出流out”作為PrintStream的輸出流,自動flush,采用charsetName字符集。 PrintStream(OutputStream out, boolean autoFlush, String charsetName) // 創建file對應的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用默認字符集。 PrintStream(File file) // 創建file對應的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用charsetName字符集。 PrintStream(File file, String charsetName) // 創建fileName對應的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用默認字符集。 PrintStream(String fileName) // 創建fileName對應的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用charsetName字符集。 PrintStream(String fileName, String charsetName) // 將“字符c”追加到“PrintStream輸出流中” PrintStream append(char c) // 將“字符序列從start(包括)到end(不包括)的全部字符”追加到“PrintStream輸出流中” PrintStream append(CharSequence charSequence, int start, int end) // 將“字符序列的全部字符”追加到“PrintStream輸出流中” PrintStream append(CharSequence charSequence) // flush“PrintStream輸出流緩沖中的數據”,並檢查錯誤 boolean checkError() // 關閉“PrintStream輸出流” synchronized void close() // flush“PrintStream輸出流緩沖中的數據”。 // 例如,PrintStream裝飾的是FileOutputStream,則調用flush時會將數據寫入到文件中 synchronized void flush() // 根據“Locale值(區域屬性)”來格式化數據 PrintStream format(Locale l, String format, Object... args) // 根據“默認的Locale值(區域屬性)”來格式化數據 PrintStream format(String format, Object... args) // 將“float數據f對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 void print(float f) // 將“double數據d對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 void print(double d) // 將“字符串數據str”寫入到“PrintStream輸出流”中,print實際調用的是write函數 synchronized void print(String str) // 將“對象o對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 void print(Object o) // 將“字符c對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 void print(char c) // 將“字符數組chars對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 void print(char[] chars) // 將“long型數據l對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 void print(long l) // 將“int數據i對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 void print(int i) // 將“boolean數據b對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 void print(boolean b) // 將“數據args”根據“Locale值(區域屬性)”按照format格式化,並寫入到“PrintStream輸出流”中 PrintStream printf(Locale l, String format, Object... args) // 將“數據args”根據“默認Locale值(區域屬性)”按照format格式化,並寫入到“PrintStream輸出流”中 PrintStream printf(String format, Object... args) // 將“換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 void println() // 將“float數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 void println(float f) // 將“int數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 void println(int i) // 將“long數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 void println(long l) // 將“對象o對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 void println(Object o) // 將“字符數組chars對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 void println(char[] chars) // 將“字符串str+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 synchronized void println(String str) // 將“字符c對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 void println(char c) // 將“double數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 void println(double d) // 將“boolean數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 void println(boolean b) // 將數據oneByte寫入到“PrintStream輸出流”中。oneByte雖然是int類型,但實際只會寫入一個字節 synchronized void write(int oneByte) // 將“buffer中從offset開始的length個字節”寫入到“PrintStream輸出流”中。 void write(byte[] buffer, int offset, int length)
注意:print()和println()都是將其中參數轉換成字符串之后,再寫入到輸入流。
例如,
print(0x61);
等價於
write(String.valueOf(0x61));
上面語句是將字符串"97"寫入到輸出流。0x61對應十進制數是97。
write(0x61)
上面語句是將字符'a'寫入到輸出流。因為0x61對應的ASCII碼的字母'a'。
查看下面的代碼,我們能對這些函數有更清晰的認識!
PrintStream 源碼分析(基於jdk1.7.40)

1 package java.io; 2 3 import java.util.Formatter; 4 import java.util.Locale; 5 import java.nio.charset.Charset; 6 import java.nio.charset.IllegalCharsetNameException; 7 import java.nio.charset.UnsupportedCharsetException; 8 9 public class PrintStream extends FilterOutputStream 10 implements Appendable, Closeable 11 { 12 13 // 自動flush 14 // 所謂“自動flush”,就是每次執行print(), println(), write()函數,都會調用flush()函數; 15 // 而“不自動flush”,則需要我們手動調用flush()接口。 16 private final boolean autoFlush; 17 // PrintStream是否右產生異常。當PrintStream有異常產生時,會被本身捕獲,並設置trouble為true 18 private boolean trouble = false; 19 // 用於格式化的對象 20 private Formatter formatter; 21 22 // BufferedWriter對象,用於實現“PrintStream支持字符集”。 23 // 因為PrintStream是OutputStream的子類,所以它本身不支持字符串; 24 // 但是BufferedWriter支持字符集,因此可以通過OutputStreamWriter創建PrintStream對應的BufferedWriter對象,從而支持字符集。 25 private BufferedWriter textOut; 26 private OutputStreamWriter charOut; 27 28 private static <T> T requireNonNull(T obj, String message) { 29 if (obj == null) 30 throw new NullPointerException(message); 31 return obj; 32 } 33 34 // 返回csn對應的字符集對象 35 private static Charset toCharset(String csn) 36 throws UnsupportedEncodingException 37 { 38 requireNonNull(csn, "charsetName"); 39 try { 40 return Charset.forName(csn); 41 } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) { 42 // UnsupportedEncodingException should be thrown 43 throw new UnsupportedEncodingException(csn); 44 } 45 } 46 47 // 將“輸出流out”作為PrintStream的輸出流,autoFlush的flush模式,並且采用默認字符集。 48 private PrintStream(boolean autoFlush, OutputStream out) { 49 super(out); 50 this.autoFlush = autoFlush; 51 this.charOut = new OutputStreamWriter(this); 52 this.textOut = new BufferedWriter(charOut); 53 } 54 55 // 將“輸出流out”作為PrintStream的輸出流,自動flush,采用charsetName字符集。 56 private PrintStream(boolean autoFlush, OutputStream out, Charset charset) { 57 super(out); 58 this.autoFlush = autoFlush; 59 this.charOut = new OutputStreamWriter(this, charset); 60 this.textOut = new BufferedWriter(charOut); 61 } 62 63 // 將“輸出流out”作為PrintStream的輸出流,自動flush,采用charsetName字符集。 64 private PrintStream(boolean autoFlush, Charset charset, OutputStream out) 65 throws UnsupportedEncodingException 66 { 67 this(autoFlush, out, charset); 68 } 69 70 // 將“輸出流out”作為PrintStream的輸出流,不會自動flush,並且采用默認字符集 71 public PrintStream(OutputStream out) { 72 this(out, false); 73 } 74 75 // 將“輸出流out”作為PrintStream的輸出流,自動flush,並且采用默認字符集。 76 public PrintStream(OutputStream out, boolean autoFlush) { 77 this(autoFlush, requireNonNull(out, "Null output stream")); 78 } 79 80 // 將“輸出流out”作為PrintStream的輸出流,自動flush,采用charsetName字符集。 81 public PrintStream(OutputStream out, boolean autoFlush, String encoding) 82 throws UnsupportedEncodingException 83 { 84 this(autoFlush, 85 requireNonNull(out, "Null output stream"), 86 toCharset(encoding)); 87 } 88 89 // 創建fileName對應的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用默認字符集。 90 public PrintStream(String fileName) throws FileNotFoundException { 91 this(false, new FileOutputStream(fileName)); 92 } 93 94 // 創建fileName對應的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用charsetName字符集。 95 public PrintStream(String fileName, String csn) 96 throws FileNotFoundException, UnsupportedEncodingException 97 { 98 // ensure charset is checked before the file is opened 99 this(false, toCharset(csn), new FileOutputStream(fileName)); 100 } 101 102 // 創建file對應的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用默認字符集。 103 public PrintStream(File file) throws FileNotFoundException { 104 this(false, new FileOutputStream(file)); 105 } 106 107 // 創建file對應的FileOutputStream,然后將該FileOutputStream作為PrintStream的輸出流,不自動flush,采用csn字符集。 108 public PrintStream(File file, String csn) 109 throws FileNotFoundException, UnsupportedEncodingException 110 { 111 // ensure charset is checked before the file is opened 112 this(false, toCharset(csn), new FileOutputStream(file)); 113 } 114 115 private void ensureOpen() throws IOException { 116 if (out == null) 117 throw new IOException("Stream closed"); 118 } 119 120 // flush“PrintStream輸出流緩沖中的數據”。 121 // 例如,PrintStream裝飾的是FileOutputStream,則調用flush時會將數據寫入到文件中 122 public void flush() { 123 synchronized (this) { 124 try { 125 ensureOpen(); 126 out.flush(); 127 } 128 catch (IOException x) { 129 trouble = true; 130 } 131 } 132 } 133 134 private boolean closing = false; /* To avoid recursive closing */ 135 136 // 關閉PrintStream 137 public void close() { 138 synchronized (this) { 139 if (! closing) { 140 closing = true; 141 try { 142 textOut.close(); 143 out.close(); 144 } 145 catch (IOException x) { 146 trouble = true; 147 } 148 textOut = null; 149 charOut = null; 150 out = null; 151 } 152 } 153 } 154 155 // flush“PrintStream輸出流緩沖中的數據”,並檢查錯誤 156 public boolean checkError() { 157 if (out != null) 158 flush(); 159 if (out instanceof java.io.PrintStream) { 160 PrintStream ps = (PrintStream) out; 161 return ps.checkError(); 162 } 163 return trouble; 164 } 165 166 protected void setError() { 167 trouble = true; 168 } 169 170 protected void clearError() { 171 trouble = false; 172 } 173 174 // 將數據b寫入到“PrintStream輸出流”中。b雖然是int類型,但實際只會寫入一個字節 175 public void write(int b) { 176 try { 177 synchronized (this) { 178 ensureOpen(); 179 out.write(b); 180 if ((b == '\n') && autoFlush) 181 out.flush(); 182 } 183 } 184 catch (InterruptedIOException x) { 185 Thread.currentThread().interrupt(); 186 } 187 catch (IOException x) { 188 trouble = true; 189 } 190 } 191 192 // 將“buf中從off開始的length個字節”寫入到“PrintStream輸出流”中。 193 public void write(byte buf[], int off, int len) { 194 try { 195 synchronized (this) { 196 ensureOpen(); 197 out.write(buf, off, len); 198 if (autoFlush) 199 out.flush(); 200 } 201 } 202 catch (InterruptedIOException x) { 203 Thread.currentThread().interrupt(); 204 } 205 catch (IOException x) { 206 trouble = true; 207 } 208 } 209 210 // 將“buf中的全部數據”寫入到“PrintStream輸出流”中。 211 private void write(char buf[]) { 212 try { 213 synchronized (this) { 214 ensureOpen(); 215 textOut.write(buf); 216 textOut.flushBuffer(); 217 charOut.flushBuffer(); 218 if (autoFlush) { 219 for (int i = 0; i < buf.length; i++) 220 if (buf[i] == '\n') 221 out.flush(); 222 } 223 } 224 } 225 catch (InterruptedIOException x) { 226 Thread.currentThread().interrupt(); 227 } 228 catch (IOException x) { 229 trouble = true; 230 } 231 } 232 233 // 將“字符串s”寫入到“PrintStream輸出流”中。 234 private void write(String s) { 235 try { 236 synchronized (this) { 237 ensureOpen(); 238 textOut.write(s); 239 textOut.flushBuffer(); 240 charOut.flushBuffer(); 241 if (autoFlush && (s.indexOf('\n') >= 0)) 242 out.flush(); 243 } 244 } 245 catch (InterruptedIOException x) { 246 Thread.currentThread().interrupt(); 247 } 248 catch (IOException x) { 249 trouble = true; 250 } 251 } 252 253 // 將“換行符”寫入到“PrintStream輸出流”中。 254 private void newLine() { 255 try { 256 synchronized (this) { 257 ensureOpen(); 258 textOut.newLine(); 259 textOut.flushBuffer(); 260 charOut.flushBuffer(); 261 if (autoFlush) 262 out.flush(); 263 } 264 } 265 catch (InterruptedIOException x) { 266 Thread.currentThread().interrupt(); 267 } 268 catch (IOException x) { 269 trouble = true; 270 } 271 } 272 273 // 將“boolean數據對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 274 public void print(boolean b) { 275 write(b ? "true" : "false"); 276 } 277 278 // 將“字符c對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 279 public void print(char c) { 280 write(String.valueOf(c)); 281 } 282 283 // 將“int數據i對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 284 public void print(int i) { 285 write(String.valueOf(i)); 286 } 287 288 // 將“long型數據l對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 289 public void print(long l) { 290 write(String.valueOf(l)); 291 } 292 293 // 將“float數據f對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 294 public void print(float f) { 295 write(String.valueOf(f)); 296 } 297 298 // 將“double數據d對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 299 public void print(double d) { 300 write(String.valueOf(d)); 301 } 302 303 // 將“字符數組s”寫入到“PrintStream輸出流”中,print實際調用的是write函數 304 public void print(char s[]) { 305 write(s); 306 } 307 308 // 將“字符串數據s”寫入到“PrintStream輸出流”中,print實際調用的是write函數 309 public void print(String s) { 310 if (s == null) { 311 s = "null"; 312 } 313 write(s); 314 } 315 316 // 將“對象obj對應的字符串”寫入到“PrintStream輸出流”中,print實際調用的是write函數 317 public void print(Object obj) { 318 write(String.valueOf(obj)); 319 } 320 321 322 // 將“換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 323 public void println() { 324 newLine(); 325 } 326 327 // 將“boolean數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 328 public void println(boolean x) { 329 synchronized (this) { 330 print(x); 331 newLine(); 332 } 333 } 334 335 // 將“字符x對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 336 public void println(char x) { 337 synchronized (this) { 338 print(x); 339 newLine(); 340 } 341 } 342 343 // 將“int數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 344 public void println(int x) { 345 synchronized (this) { 346 print(x); 347 newLine(); 348 } 349 } 350 351 // 將“long數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 352 public void println(long x) { 353 synchronized (this) { 354 print(x); 355 newLine(); 356 } 357 } 358 359 // 將“float數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 360 public void println(float x) { 361 synchronized (this) { 362 print(x); 363 newLine(); 364 } 365 } 366 367 // 將“double數據對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 368 public void println(double x) { 369 synchronized (this) { 370 print(x); 371 newLine(); 372 } 373 } 374 375 // 將“字符數組x+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 376 public void println(char x[]) { 377 synchronized (this) { 378 print(x); 379 newLine(); 380 } 381 } 382 383 // 將“字符串x+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 384 public void println(String x) { 385 synchronized (this) { 386 print(x); 387 newLine(); 388 } 389 } 390 391 // 將“對象o對應的字符串+換行符”寫入到“PrintStream輸出流”中,println實際調用的是write函數 392 public void println(Object x) { 393 String s = String.valueOf(x); 394 synchronized (this) { 395 print(s); 396 newLine(); 397 } 398 } 399 400 // 將“數據args”根據“默認Locale值(區域屬性)”按照format格式化,並寫入到“PrintStream輸出流”中 401 public PrintStream printf(String format, Object ... args) { 402 return format(format, args); 403 } 404 405 // 將“數據args”根據“Locale值(區域屬性)”按照format格式化,並寫入到“PrintStream輸出流”中 406 public PrintStream printf(Locale l, String format, Object ... args) { 407 return format(l, format, args); 408 } 409 410 // 根據“默認的Locale值(區域屬性)”來格式化數據 411 public PrintStream format(String format, Object ... args) { 412 try { 413 synchronized (this) { 414 ensureOpen(); 415 if ((formatter == null) 416 || (formatter.locale() != Locale.getDefault())) 417 formatter = new Formatter((Appendable) this); 418 formatter.format(Locale.getDefault(), format, args); 419 } 420 } catch (InterruptedIOException x) { 421 Thread.currentThread().interrupt(); 422 } catch (IOException x) { 423 trouble = true; 424 } 425 return this; 426 } 427 428 // 根據“Locale值(區域屬性)”來格式化數據 429 public PrintStream format(Locale l, String format, Object ... args) { 430 try { 431 synchronized (this) { 432 ensureOpen(); 433 if ((formatter == null) 434 || (formatter.locale() != l)) 435 formatter = new Formatter(this, l); 436 formatter.format(l, format, args); 437 } 438 } catch (InterruptedIOException x) { 439 Thread.currentThread().interrupt(); 440 } catch (IOException x) { 441 trouble = true; 442 } 443 return this; 444 } 445 446 // 將“字符序列的全部字符”追加到“PrintStream輸出流中” 447 public PrintStream append(CharSequence csq) { 448 if (csq == null) 449 print("null"); 450 else 451 print(csq.toString()); 452 return this; 453 } 454 455 // 將“字符序列從start(包括)到end(不包括)的全部字符”追加到“PrintStream輸出流中” 456 public PrintStream append(CharSequence csq, int start, int end) { 457 CharSequence cs = (csq == null ? "null" : csq); 458 write(cs.subSequence(start, end).toString()); 459 return this; 460 } 461 462 // 將“字符c”追加到“PrintStream輸出流中” 463 public PrintStream append(char c) { 464 print(c); 465 return this; 466 } 467 }
說明:
PrintStream的源碼比較簡單,請上文的注釋進行閱讀。若有不明白的地方,建議先看看后面的PrintStream使用示例;待搞清它的作用和用法之后,再來閱讀源碼。
PrintStream和DataOutputStream異同點
相同點:都是繼承與FileOutputStream,用於包裝其它輸出流。
不同點:
(01) PrintStream和DataOutputStream 都可以將數據格式化輸出;但它們在“輸出字符串”時的編碼不同。
PrintStream是輸出時采用的是用戶指定的編碼(創建PrintStream時指定的),若沒有指定,則采用系統默認的字符編碼。而DataOutputStream則采用的是UTF-8。
關於UTF-8的字符編碼可以參考“字符編碼(ASCII,Unicode和UTF-8) 和 大小端”
關於DataOutputStream的更多內容,可以參考“java io系列15之 DataOutputStream(數據輸出流)的認知、源碼和示例”
(02) 它們的寫入數據時的異常處理機制不同。
DataOutputStream在通過write()向“輸出流”中寫入數據時,若產生IOException,會拋出。
而PrintStream在通過write()向“輸出流”中寫入數據時,若產生IOException,則會在write()中進行捕獲處理;並設置trouble標記(用於表示產生了異常)為true。用戶可以通過checkError()返回trouble值,從而檢查輸出流中是否產生了異常。
(03) 構造函數不同
DataOutputStream的構造函數只有一個:DataOutputStream(OutputStream out)。即它只支持以輸出流out作為“DataOutputStream的輸出流”。
而PrintStream的構造函數有許多:和DataOutputStream一樣,支持以輸出流out作為“PrintStream輸出流”的構造函數;還支持以“File對象”或者“String類型的文件名對象”的構造函數。
而且,在PrintStream的構造函數中,能“指定字符集”和“是否支持自動flush()操作”。
(04) 目的不同
DataOutputStream的作用是裝飾其它的輸出流,它和DataInputStream配合使用:允許應用程序以與機器無關的方式從底層輸入流中讀寫java數據類型。
而PrintStream的作用雖然也是裝飾其他輸出流,但是它的目的不是以與機器無關的方式從底層讀寫java數據類型;而是為其它輸出流提供打印各種數據值表示形式,使其它輸出流能方便的通過print(), println()或printf()等輸出各種格式的數據。
示例代碼
關於PrintStream中API的詳細用法,參考示例代碼(PrintStreamTest.java):
1 import java.io.PrintStream; 2 import java.io.File; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 6 /** 7 * PrintStream 的示例程序 8 * 9 * @author skywang 10 */ 11 public class PrintStreamTest { 12 13 public static void main(String[] args) { 14 15 // 下面3個函數的作用都是一樣:都是將字母“abcde”寫入到文件“file.txt”中。 16 // 任選一個執行即可! 17 testPrintStreamConstrutor1() ; 18 //testPrintStreamConstrutor2() ; 19 //testPrintStreamConstrutor3() ; 20 21 // 測試write(), print(), println(), printf()等接口。 22 testPrintStreamAPIS() ; 23 } 24 25 /** 26 * PrintStream(OutputStream out) 的測試函數 27 * 28 * 函數的作用,就是將字母“abcde”寫入到文件“file.txt”中 29 */ 30 private static void testPrintStreamConstrutor1() { 31 // 0x61對應ASCII碼的字母'a',0x62對應ASCII碼的字母'b', ... 32 final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 }; // abced 33 try { 34 // 創建文件“file.txt”的File對象 35 File file = new File("file.txt"); 36 // 創建文件對應FileOutputStream 37 PrintStream out = new PrintStream( 38 new FileOutputStream(file)); 39 // 將“字節數組arr”全部寫入到輸出流中 40 out.write(arr); 41 // 關閉輸出流 42 out.close(); 43 } catch (IOException e) { 44 e.printStackTrace(); 45 } 46 } 47 48 /** 49 * PrintStream(File file) 的測試函數 50 * 51 * 函數的作用,就是將字母“abcde”寫入到文件“file.txt”中 52 */ 53 private static void testPrintStreamConstrutor2() { 54 final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 }; 55 try { 56 File file = new File("file.txt"); 57 PrintStream out = new PrintStream(file); 58 out.write(arr); 59 out.close(); 60 } catch (IOException e) { 61 e.printStackTrace(); 62 } 63 } 64 65 /** 66 * PrintStream(String fileName) 的測試函數 67 * 68 * 函數的作用,就是將字母“abcde”寫入到文件“file.txt”中 69 */ 70 private static void testPrintStreamConstrutor3() { 71 final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 }; 72 try { 73 PrintStream out = new PrintStream("file.txt"); 74 out.write(arr); 75 out.close(); 76 } catch (IOException e) { 77 e.printStackTrace(); 78 } 79 } 80 81 /** 82 * 測試write(), print(), println(), printf()等接口。 83 */ 84 private static void testPrintStreamAPIS() { 85 // 0x61對應ASCII碼的字母'a',0x62對應ASCII碼的字母'b', ... 86 final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 }; // abced 87 try { 88 // 創建文件對應FileOutputStream 89 PrintStream out = new PrintStream("other.txt"); 90 91 // 將字符串“hello PrintStream”+回車符,寫入到輸出流中 92 out.println("hello PrintStream"); 93 // 將0x41寫入到輸出流中 94 // 0x41對應ASCII碼的字母'A',也就是寫入字符'A' 95 out.write(0x41); 96 // 將字符串"65"寫入到輸出流中。 97 // out.print(0x41); 等價於 out.write(String.valueOf(0x41)); 98 out.print(0x41); 99 // 將字符'B'追加到輸出流中 100 out.append('B'); 101 102 // 將"CDE is 5" + 回車 寫入到輸出流中 103 String str = "CDE"; 104 int num = 5; 105 out.printf("%s is %d\n", str, num); 106 107 out.close(); 108 } catch (IOException e) { 109 e.printStackTrace(); 110 } 111 } 112 }
運行上面的代碼,會在源碼所在目錄生成兩個文件“file.txt”和“other.txt”。
file.txt的內容如下:
abcde
other.txt的內容如下:
hello PrintStream
A65BCDE is 5