更多內容請參考:java io系列01之 "目錄"
PrintWriter 介紹
PrintWriter 是字符類型的打印輸出流,它繼承於Writer。
PrintStream 用於向文本輸出流打印對象的格式化表示形式。它實現在 PrintStream 中的所有 print 方法。它不包含用於寫入原始字節的方法,對於這些字節,程序應該使用未編碼的字節流進行寫入。
PrintWriter 函數列表
PrintWriter(OutputStream out) PrintWriter(OutputStream out, boolean autoFlush) PrintWriter(Writer wr) PrintWriter(Writer wr, boolean autoFlush) PrintWriter(File file) PrintWriter(File file, String csn) PrintWriter(String fileName) PrintWriter(String fileName, String csn) PrintWriter append(char c) PrintWriter append(CharSequence csq, int start, int end) PrintWriter append(CharSequence csq) boolean checkError() void close() void flush() PrintWriter format(Locale l, String format, Object... args) PrintWriter format(String format, Object... args) void print(float fnum) void print(double dnum) void print(String str) void print(Object obj) void print(char ch) void print(char[] charArray) void print(long lnum) void print(int inum) void print(boolean bool) PrintWriter printf(Locale l, String format, Object... args) PrintWriter printf(String format, Object... args) void println() void println(float f) void println(int i) void println(long l) void println(Object obj) void println(char[] chars) void println(String str) void println(char c) void println(double d) void println(boolean b) void write(char[] buf, int offset, int count) void write(int oneChar) void write(char[] buf) void write(String str, int offset, int count) void write(String str)
PrintWriter 源碼
1 package java.io; 2 3 import java.util.Objects; 4 import java.util.Formatter; 5 import java.util.Locale; 6 import java.nio.charset.Charset; 7 import java.nio.charset.IllegalCharsetNameException; 8 import java.nio.charset.UnsupportedCharsetException; 9 10 public class PrintWriter extends Writer { 11 12 protected Writer out; 13 14 // 自動flush 15 // 所謂“自動flush”,就是每次執行print(), println(), write()函數,都會調用flush()函數; 16 // 而“不自動flush”,則需要我們手動調用flush()接口。 17 private final boolean autoFlush; 18 // PrintWriter是否右產生異常。當PrintWriter有異常產生時,會被本身捕獲,並設置trouble為true 19 private boolean trouble = false; 20 // 用於格式化的對象 21 private Formatter formatter; 22 private PrintStream psOut = null; 23 24 // 行分割符 25 private final String lineSeparator; 26 27 // 獲取csn(字符集名字)對應的Chaset 28 private static Charset toCharset(String csn) 29 throws UnsupportedEncodingException 30 { 31 Objects.requireNonNull(csn, "charsetName"); 32 try { 33 return Charset.forName(csn); 34 } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) { 35 // UnsupportedEncodingException should be thrown 36 throw new UnsupportedEncodingException(csn); 37 } 38 } 39 40 // 將“Writer對象out”作為PrintWriter的輸出流,默認不會自動flush,並且采用默認字符集。 41 public PrintWriter (Writer out) { 42 this(out, false); 43 } 44 45 // 將“Writer對象out”作為PrintWriter的輸出流,autoFlush的flush模式,並且采用默認字符集。 46 public PrintWriter(Writer out, boolean autoFlush) { 47 super(out); 48 this.out = out; 49 this.autoFlush = autoFlush; 50 lineSeparator = java.security.AccessController.doPrivileged( 51 new sun.security.action.GetPropertyAction("line.separator")); 52 } 53 54 // 將“輸出流對象out”作為PrintWriter的輸出流,不自動flush,並且采用默認字符集。 55 public PrintWriter(OutputStream out) { 56 this(out, false); 57 } 58 59 // 將“輸出流對象out”作為PrintWriter的輸出流,autoFlush的flush模式,並且采用默認字符集。 60 public PrintWriter(OutputStream out, boolean autoFlush) { 61 // new OutputStreamWriter(out):將“字節類型的輸出流”轉換為“字符類型的輸出流” 62 // new BufferedWriter(...): 為輸出流提供緩沖功能。 63 this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush); 64 65 // save print stream for error propagation 66 if (out instanceof java.io.PrintStream) { 67 psOut = (PrintStream) out; 68 } 69 } 70 71 // 創建fileName對應的OutputStreamWriter,進而創建BufferedWriter對象;然后將該BufferedWriter作為PrintWriter的輸出流,不自動flush,采用默認字符集。 72 public PrintWriter(String fileName) throws FileNotFoundException { 73 this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))), 74 false); 75 } 76 77 // 創建fileName對應的OutputStreamWriter,進而創建BufferedWriter對象;然后將該BufferedWriter作為PrintWriter的輸出流,不自動flush,采用字符集charset。 78 private PrintWriter(Charset charset, File file) 79 throws FileNotFoundException 80 { 81 this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)), 82 false); 83 } 84 85 // 創建fileName對應的OutputStreamWriter,進而創建BufferedWriter對象;然后將該BufferedWriter作為PrintWriter的輸出流,不自動flush,采用csn字符集。 86 public PrintWriter(String fileName, String csn) 87 throws FileNotFoundException, UnsupportedEncodingException 88 { 89 this(toCharset(csn), new File(fileName)); 90 } 91 92 // 創建file對應的OutputStreamWriter,進而創建BufferedWriter對象;然后將該BufferedWriter作為PrintWriter的輸出流,不自動flush,采用默認字符集。 93 public PrintWriter(File file) throws FileNotFoundException { 94 this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))), 95 false); 96 } 97 98 // 創建file對應的OutputStreamWriter,進而創建BufferedWriter對象;然后將該BufferedWriter作為PrintWriter的輸出流,不自動flush,采用csn字符集。 99 public PrintWriter(File file, String csn) 100 throws FileNotFoundException, UnsupportedEncodingException 101 { 102 this(toCharset(csn), file); 103 } 104 105 private void ensureOpen() throws IOException { 106 if (out == null) 107 throw new IOException("Stream closed"); 108 } 109 110 // flush“PrintWriter輸出流中的數據”。 111 public void flush() { 112 try { 113 synchronized (lock) { 114 ensureOpen(); 115 out.flush(); 116 } 117 } 118 catch (IOException x) { 119 trouble = true; 120 } 121 } 122 123 public void close() { 124 try { 125 synchronized (lock) { 126 if (out == null) 127 return; 128 out.close(); 129 out = null; 130 } 131 } 132 catch (IOException x) { 133 trouble = true; 134 } 135 } 136 137 // flush“PrintWriter輸出流緩沖中的數據”,並檢查錯誤 138 public boolean checkError() { 139 if (out != null) { 140 flush(); 141 } 142 if (out instanceof java.io.PrintWriter) { 143 PrintWriter pw = (PrintWriter) out; 144 return pw.checkError(); 145 } else if (psOut != null) { 146 return psOut.checkError(); 147 } 148 return trouble; 149 } 150 151 protected void setError() { 152 trouble = true; 153 } 154 155 protected void clearError() { 156 trouble = false; 157 } 158 159 // 將字符c寫入到“PrintWriter輸出流”中。c雖然是int類型,但實際只會寫入一個字符 160 public void write(int c) { 161 try { 162 synchronized (lock) { 163 ensureOpen(); 164 out.write(c); 165 } 166 } 167 catch (InterruptedIOException x) { 168 Thread.currentThread().interrupt(); 169 } 170 catch (IOException x) { 171 trouble = true; 172 } 173 } 174 175 // 將“buf中從off開始的len個字符”寫入到“PrintWriter輸出流”中。 176 public void write(char buf[], int off, int len) { 177 try { 178 synchronized (lock) { 179 ensureOpen(); 180 out.write(buf, off, len); 181 } 182 } 183 catch (InterruptedIOException x) { 184 Thread.currentThread().interrupt(); 185 } 186 catch (IOException x) { 187 trouble = true; 188 } 189 } 190 191 // 將“buf中的全部數據”寫入到“PrintWriter輸出流”中。 192 public void write(char buf[]) { 193 write(buf, 0, buf.length); 194 } 195 196 // 將“字符串s中從off開始的len個字符”寫入到“PrintWriter輸出流”中。 197 public void write(String s, int off, int len) { 198 try { 199 synchronized (lock) { 200 ensureOpen(); 201 out.write(s, off, len); 202 } 203 } 204 catch (InterruptedIOException x) { 205 Thread.currentThread().interrupt(); 206 } 207 catch (IOException x) { 208 trouble = true; 209 } 210 } 211 212 // 將“字符串s”寫入到“PrintWriter輸出流”中。 213 public void write(String s) { 214 write(s, 0, s.length()); 215 } 216 217 // 將“換行符”寫入到“PrintWriter輸出流”中。 218 private void newLine() { 219 try { 220 synchronized (lock) { 221 ensureOpen(); 222 out.write(lineSeparator); 223 if (autoFlush) 224 out.flush(); 225 } 226 } 227 catch (InterruptedIOException x) { 228 Thread.currentThread().interrupt(); 229 } 230 catch (IOException x) { 231 trouble = true; 232 } 233 } 234 235 // 將“boolean數據對應的字符串”寫入到“PrintWriter輸出流”中,print實際調用的是write函數 236 public void print(boolean b) { 237 write(b ? "true" : "false"); 238 } 239 240 // 將“字符c對應的字符串”寫入到“PrintWriter輸出流”中,print實際調用的是write函數 241 public void print(char c) { 242 write(c); 243 } 244 245 // 將“int數據i對應的字符串”寫入到“PrintWriter輸出流”中,print實際調用的是write函數 246 public void print(int i) { 247 write(String.valueOf(i)); 248 } 249 250 // 將“long型數據l對應的字符串”寫入到“PrintWriter輸出流”中,print實際調用的是write函數 251 public void print(long l) { 252 write(String.valueOf(l)); 253 } 254 255 // 將“float數據f對應的字符串”寫入到“PrintWriter輸出流”中,print實際調用的是write函數 256 public void print(float f) { 257 write(String.valueOf(f)); 258 } 259 260 // 將“double數據d對應的字符串”寫入到“PrintWriter輸出流”中,print實際調用的是write函數 261 public void print(double d) { 262 write(String.valueOf(d)); 263 } 264 265 // 將“字符數組s”寫入到“PrintWriter輸出流”中,print實際調用的是write函數 266 public void print(char s[]) { 267 write(s); 268 } 269 270 // 將“字符串數據s”寫入到“PrintWriter輸出流”中,print實際調用的是write函數 271 public void print(String s) { 272 if (s == null) { 273 s = "null"; 274 } 275 write(s); 276 } 277 278 // 將“對象obj對應的字符串”寫入到“PrintWriter輸出流”中,print實際調用的是write函數 279 public void print(Object obj) { 280 write(String.valueOf(obj)); 281 } 282 283 // 將“換行符”寫入到“PrintWriter輸出流”中,println實際調用的是write函數 284 public void println() { 285 newLine(); 286 } 287 288 // 將“boolean數據對應的字符串+換行符”寫入到“PrintWriter輸出流”中,println實際調用的是write函數 289 public void println(boolean x) { 290 synchronized (lock) { 291 print(x); 292 println(); 293 } 294 } 295 296 // 將“字符x對應的字符串+換行符”寫入到“PrintWriter輸出流”中,println實際調用的是write函數 297 public void println(char x) { 298 synchronized (lock) { 299 print(x); 300 println(); 301 } 302 } 303 304 // 將“int數據對應的字符串+換行符”寫入到“PrintWriter輸出流”中,println實際調用的是write函數 305 public void println(int x) { 306 synchronized (lock) { 307 print(x); 308 println(); 309 } 310 } 311 312 // 將“long數據對應的字符串+換行符”寫入到“PrintWriter輸出流”中,println實際調用的是write函數 313 public void println(long x) { 314 synchronized (lock) { 315 print(x); 316 println(); 317 } 318 } 319 320 // 將“float數據對應的字符串+換行符”寫入到“PrintWriter輸出流”中,println實際調用的是write函數 321 public void println(float x) { 322 synchronized (lock) { 323 print(x); 324 println(); 325 } 326 } 327 328 // 將“double數據對應的字符串+換行符”寫入到“PrintWriter輸出流”中,println實際調用的是write函數 329 public void println(double x) { 330 synchronized (lock) { 331 print(x); 332 println(); 333 } 334 } 335 336 // 將“字符數組x+換行符”寫入到“PrintWriter輸出流”中,println實際調用的是write函數 337 public void println(char x[]) { 338 synchronized (lock) { 339 print(x); 340 println(); 341 } 342 } 343 344 // 將“字符串x+換行符”寫入到“PrintWriter輸出流”中,println實際調用的是write函數 345 public void println(String x) { 346 synchronized (lock) { 347 print(x); 348 println(); 349 } 350 } 351 352 // 將“對象o對應的字符串+換行符”寫入到“PrintWriter輸出流”中,println實際調用的是write函數 353 public void println(Object x) { 354 String s = String.valueOf(x); 355 synchronized (lock) { 356 print(s); 357 println(); 358 } 359 } 360 361 // 將“數據args”根據“默認Locale值(區域屬性)”按照format格式化,並寫入到“PrintWriter輸出流”中 362 public PrintWriter printf(String format, Object ... args) { 363 return format(format, args); 364 } 365 366 // 將“數據args”根據“Locale值(區域屬性)”按照format格式化,並寫入到“PrintWriter輸出流”中 367 public PrintWriter printf(Locale l, String format, Object ... args) { 368 return format(l, format, args); 369 } 370 371 // 根據“默認的Locale值(區域屬性)”來格式化數據 372 public PrintWriter format(String format, Object ... args) { 373 try { 374 synchronized (lock) { 375 ensureOpen(); 376 if ((formatter == null) 377 || (formatter.locale() != Locale.getDefault())) 378 formatter = new Formatter(this); 379 formatter.format(Locale.getDefault(), format, args); 380 if (autoFlush) 381 out.flush(); 382 } 383 } catch (InterruptedIOException x) { 384 Thread.currentThread().interrupt(); 385 } catch (IOException x) { 386 trouble = true; 387 } 388 return this; 389 } 390 391 // 根據“Locale值(區域屬性)”來格式化數據 392 public PrintWriter format(Locale l, String format, Object ... args) { 393 try { 394 synchronized (lock) { 395 ensureOpen(); 396 if ((formatter == null) || (formatter.locale() != l)) 397 formatter = new Formatter(this, l); 398 formatter.format(l, format, args); 399 if (autoFlush) 400 out.flush(); 401 } 402 } catch (InterruptedIOException x) { 403 Thread.currentThread().interrupt(); 404 } catch (IOException x) { 405 trouble = true; 406 } 407 return this; 408 } 409 410 // 將“字符序列的全部字符”追加到“PrintWriter輸出流中” 411 public PrintWriter append(CharSequence csq) { 412 if (csq == null) 413 write("null"); 414 else 415 write(csq.toString()); 416 return this; 417 } 418 419 // 將“字符序列從start(包括)到end(不包括)的全部字符”追加到“PrintWriter輸出流中” 420 public PrintWriter append(CharSequence csq, int start, int end) { 421 CharSequence cs = (csq == null ? "null" : csq); 422 write(cs.subSequence(start, end).toString()); 423 return this; 424 } 425 426 // 將“字符c”追加到“PrintWriter輸出流中” 427 public PrintWriter append(char c) { 428 write(c); 429 return this; 430 } 431 }
示例代碼
關於PrintWriter中API的詳細用法,參考示例代碼(PrintWriterTest.java):
1 import java.io.PrintWriter; 2 import java.io.File; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 6 /** 7 * PrintWriter 的示例程序 8 * 9 * @author skywang 10 */ 11 public class PrintWriterTest { 12 13 public static void main(String[] args) { 14 15 // 下面3個函數的作用都是一樣:都是將字母“abcde”寫入到文件“file.txt”中。 16 // 任選一個執行即可! 17 testPrintWriterConstrutor1() ; 18 //testPrintWriterConstrutor2() ; 19 //testPrintWriterConstrutor3() ; 20 21 // 測試write(), print(), println(), printf()等接口。 22 testPrintWriterAPIS() ; 23 } 24 25 /** 26 * PrintWriter(OutputStream out) 的測試函數 27 * 28 * 函數的作用,就是將字母“abcde”寫入到文件“file.txt”中 29 */ 30 private static void testPrintWriterConstrutor1() { 31 final char[] arr={'a', 'b', 'c', 'd', 'e' }; 32 try { 33 // 創建文件“file.txt”的File對象 34 File file = new File("file.txt"); 35 // 創建文件對應FileOutputStream 36 PrintWriter out = new PrintWriter( 37 new FileOutputStream(file)); 38 // 將“字節數組arr”全部寫入到輸出流中 39 out.write(arr); 40 // 關閉輸出流 41 out.close(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 47 /** 48 * PrintWriter(File file) 的測試函數 49 * 50 * 函數的作用,就是將字母“abcde”寫入到文件“file.txt”中 51 */ 52 private static void testPrintWriterConstrutor2() { 53 final char[] arr={'a', 'b', 'c', 'd', 'e' }; 54 try { 55 File file = new File("file.txt"); 56 PrintWriter out = new PrintWriter(file); 57 out.write(arr); 58 out.close(); 59 } catch (IOException e) { 60 e.printStackTrace(); 61 } 62 } 63 64 /** 65 * PrintWriter(String fileName) 的測試函數 66 * 67 * 函數的作用,就是將字母“abcde”寫入到文件“file.txt”中 68 */ 69 private static void testPrintWriterConstrutor3() { 70 final char[] arr={'a', 'b', 'c', 'd', 'e' }; 71 try { 72 PrintWriter out = new PrintWriter("file.txt"); 73 out.write(arr); 74 out.close(); 75 } catch (IOException e) { 76 e.printStackTrace(); 77 } 78 } 79 80 /** 81 * 測試write(), print(), println(), printf()等接口。 82 */ 83 private static void testPrintWriterAPIS() { 84 final char[] arr={'a', 'b', 'c', 'd', 'e' }; 85 try { 86 // 創建文件對應FileOutputStream 87 PrintWriter out = new PrintWriter("other.txt"); 88 89 // 將字符串“hello PrintWriter”+回車符,寫入到輸出流中 90 out.println("hello PrintWriter"); 91 // 將0x41寫入到輸出流中 92 // 0x41對應ASCII碼的字母'A',也就是寫入字符'A' 93 out.write(0x41); 94 // 將字符串"65"寫入到輸出流中。 95 // out.print(0x41); 等價於 out.write(String.valueOf(0x41)); 96 out.print(0x41); 97 // 將字符'B'追加到輸出流中 98 out.append('B').append("CDEF"); 99 100 // 將"CDE is 5" + 回車 寫入到輸出流中 101 String str = "GHI"; 102 int num = 5; 103 out.printf("%s is %d\n", str, num); 104 105 out.close(); 106 } catch (IOException e) { 107 e.printStackTrace(); 108 } 109 } 110 }
運行上面的代碼,會在源碼所在目錄生成兩個文件“file.txt”和“other.txt”。
file.txt的內容如下:
abcde
other.txt的內容如下:
hello PrintWriter
A65BCDEFGHI is 5
