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