服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|

服务器之家 - 编程语言 - JAVA教程 - Java 中的Printstream介绍_动力节点Java学院整理

Java 中的Printstream介绍_动力节点Java学院整理

2020-10-23 20:35动力节点 JAVA教程

PrintStream 是打印输出流,它继承于FilterOutputStream。接下来通过本文给大家介绍Java 中的Printstream,需要的朋友参考下吧

PrintStream 介绍

PrintStream 是打印输出流,它继承于FilterOutputStream。

PrintStream 是用来装饰其它输出流。它能为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。

与其他输出流不同,PrintStream 永远不会抛出 IOException;它产生的IOException会被自身的函数所捕获并设置错误标记, 用户可以通过 checkError() 返回错误标记,从而查看PrintStream内部是否产生了IOException。
另外,PrintStream 提供了自动flush 和 字符集设置功能。所谓自动flush,就是往PrintStream写入的数据会立刻调用flush()函数。

PrintStream 函数列表 

?
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
/*
 * 构造函数
 */
// 将“输出流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()都是将其中参数转换成字符串之后,再写入到输入流。

例如,

?
1
print(0x61);

等价于

?
1
write(String.valueOf(0x61));

上面语句是将字符串"97"写入到输出流。0x61对应十进制数是97。

?
1
write(0x61)

上面语句是将字符'a'写入到输出流。因为0x61对应的ASCII码的字母'a'。

查看下面的代码,我们能对这些函数有更清晰的认识! 

PrintStream 源码分析(基于jdk1.7.40) 

?
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
package java.io;
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 PrintStream extends FilterOutputStream
 implements Appendable, Closeable
{
 // 自动flush
 // 所谓“自动flush”,就是每次执行print(), println(), write()函数,都会调用flush()函数;
 // 而“不自动flush”,则需要我们手动调用flush()接口。
 private final boolean autoFlush;
 // PrintStream是否右产生异常。当PrintStream有异常产生时,会被本身捕获,并设置trouble为true
 private boolean trouble = false;
 // 用于格式化的对象
 private Formatter formatter;
 // BufferedWriter对象,用于实现“PrintStream支持字符集”。
 // 因为PrintStream是OutputStream的子类,所以它本身不支持字符串;
 // 但是BufferedWriter支持字符集,因此可以通过OutputStreamWriter创建PrintStream对应的BufferedWriter对象,从而支持字符集。
 private BufferedWriter textOut;
 private OutputStreamWriter charOut;
 private static <T> T requireNonNull(T obj, String message) {
  if (obj == null)
   throw new NullPointerException(message);
  return obj;
 }
 // 返回csn对应的字符集对象
 private static Charset toCharset(String csn)
  throws UnsupportedEncodingException
 {
  requireNonNull(csn, "charsetName");
  try {
   return Charset.forName(csn);
  } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
   // UnsupportedEncodingException should be thrown
   throw new UnsupportedEncodingException(csn);
  }
 }
 // 将“输出流out”作为PrintStream的输出流,autoFlush的flush模式,并且采用默认字符集。
 private PrintStream(boolean autoFlush, OutputStream out) {
  super(out);
  this.autoFlush = autoFlush;
  this.charOut = new OutputStreamWriter(this);
  this.textOut = new BufferedWriter(charOut);
 }
 // 将“输出流out”作为PrintStream的输出流,自动flush,采用charsetName字符集。
 private PrintStream(boolean autoFlush, OutputStream out, Charset charset) {
  super(out);
  this.autoFlush = autoFlush;
  this.charOut = new OutputStreamWriter(this, charset);
  this.textOut = new BufferedWriter(charOut);
 }
 // 将“输出流out”作为PrintStream的输出流,自动flush,采用charsetName字符集。
 private PrintStream(boolean autoFlush, Charset charset, OutputStream out)
  throws UnsupportedEncodingException
 {
  this(autoFlush, out, charset);
 }
 // 将“输出流out”作为PrintStream的输出流,不会自动flush,并且采用默认字符集
 public PrintStream(OutputStream out) {
  this(out, false);
 }
 // 将“输出流out”作为PrintStream的输出流,自动flush,并且采用默认字符集。
 public PrintStream(OutputStream out, boolean autoFlush) {
  this(autoFlush, requireNonNull(out, "Null output stream"));
 }
 // 将“输出流out”作为PrintStream的输出流,自动flush,采用charsetName字符集。
 public PrintStream(OutputStream out, boolean autoFlush, String encoding)
  throws UnsupportedEncodingException
 {
  this(autoFlush,
   requireNonNull(out, "Null output stream"),
   toCharset(encoding));
 }
 // 创建fileName对应的FileOutputStream,然后将该FileOutputStream作为PrintStream的输出流,不自动flush,采用默认字符集。
 public PrintStream(String fileName) throws FileNotFoundException {
  this(false, new FileOutputStream(fileName));
 }
 // 创建fileName对应的FileOutputStream,然后将该FileOutputStream作为PrintStream的输出流,不自动flush,采用charsetName字符集。
 public PrintStream(String fileName, String csn)
  throws FileNotFoundException, UnsupportedEncodingException
 {
  // ensure charset is checked before the file is opened
  this(false, toCharset(csn), new FileOutputStream(fileName));
 }
 // 创建file对应的FileOutputStream,然后将该FileOutputStream作为PrintStream的输出流,不自动flush,采用默认字符集。
 public PrintStream(File file) throws FileNotFoundException {
  this(false, new FileOutputStream(file));
 }
 // 创建file对应的FileOutputStream,然后将该FileOutputStream作为PrintStream的输出流,不自动flush,采用csn字符集。
 public PrintStream(File file, String csn)
  throws FileNotFoundException, UnsupportedEncodingException
 {
  // ensure charset is checked before the file is opened
  this(false, toCharset(csn), new FileOutputStream(file));
 }
 private void ensureOpen() throws IOException {
  if (out == null)
   throw new IOException("Stream closed");
 }
 // flush“PrintStream输出流缓冲中的数据”。
 // 例如,PrintStream装饰的是FileOutputStream,则调用flush时会将数据写入到文件中
 public void flush() {
  synchronized (this) {
   try {
    ensureOpen();
    out.flush();
   }
   catch (IOException x) {
    trouble = true;
   }
  }
 }
 private boolean closing = false; /* To avoid recursive closing */
 // 关闭PrintStream
 public void close() {
  synchronized (this) {
   if (! closing) {
    closing = true;
    try {
     textOut.close();
     out.close();
    }
    catch (IOException x) {
     trouble = true;
    }
    textOut = null;
    charOut = null;
    out = null;
   }
  }
 }
 // flush“PrintStream输出流缓冲中的数据”,并检查错误
 public boolean checkError() {
  if (out != null)
   flush();
  if (out instanceof java.io.PrintStream) {
   PrintStream ps = (PrintStream) out;
   return ps.checkError();
  }
  return trouble;
 }
 protected void setError() {
  trouble = true;
 }
 protected void clearError() {
  trouble = false;
 }
 // 将数据b写入到“PrintStream输出流”中。b虽然是int类型,但实际只会写入一个字节
 public void write(int b) {
  try {
   synchronized (this) {
    ensureOpen();
    out.write(b);
    if ((b == '\n') && autoFlush)
     out.flush();
   }
  }
  catch (InterruptedIOException x) {
   Thread.currentThread().interrupt();
  }
  catch (IOException x) {
   trouble = true;
  }
 }
 // 将“buf中从off开始的length个字节”写入到“PrintStream输出流”中。
 public void write(byte buf[], int off, int len) {
  try {
   synchronized (this) {
    ensureOpen();
    out.write(buf, off, len);
    if (autoFlush)
     out.flush();
   }
  }
  catch (InterruptedIOException x) {
   Thread.currentThread().interrupt();
  }
  catch (IOException x) {
   trouble = true;
  }
 }
 // 将“buf中的全部数据”写入到“PrintStream输出流”中。
 private void write(char buf[]) {
  try {
   synchronized (this) {
    ensureOpen();
    textOut.write(buf);
    textOut.flushBuffer();
    charOut.flushBuffer();
    if (autoFlush) {
     for (int i = ; i < buf.length; i++)
      if (buf[i] == '\n')
       out.flush();
    }
   }
  }
  catch (InterruptedIOException x) {
   Thread.currentThread().interrupt();
  }
  catch (IOException x) {
   trouble = true;
  }
 }
 // 将“字符串s”写入到“PrintStream输出流”中。
 private void write(String s) {
  try {
   synchronized (this) {
    ensureOpen();
    textOut.write(s);
    textOut.flushBuffer();
    charOut.flushBuffer();
    if (autoFlush && (s.indexOf('\n') >= ))
     out.flush();
   }
  }
  catch (InterruptedIOException x) {
   Thread.currentThread().interrupt();
  }
  catch (IOException x) {
   trouble = true;
  }
 }
 // 将“换行符”写入到“PrintStream输出流”中。
 private void newLine() {
  try {
   synchronized (this) {
    ensureOpen();
    textOut.newLine();
    textOut.flushBuffer();
    charOut.flushBuffer();
    if (autoFlush)
     out.flush();
   }
  }
  catch (InterruptedIOException x) {
   Thread.currentThread().interrupt();
  }
  catch (IOException x) {
   trouble = true;
  }
 }
 // 将“boolean数据对应的字符串”写入到“PrintStream输出流”中,print实际调用的是write函数
 public void print(boolean b) {
  write(b ? "true" : "false");
 }
 // 将“字符c对应的字符串”写入到“PrintStream输出流”中,print实际调用的是write函数
 public void print(char c) {
  write(String.valueOf(c));
 }
 // 将“int数据i对应的字符串”写入到“PrintStream输出流”中,print实际调用的是write函数
 public void print(int i) {
  write(String.valueOf(i));
 }
 // 将“long型数据l对应的字符串”写入到“PrintStream输出流”中,print实际调用的是write函数
 public void print(long l) {
  write(String.valueOf(l));
 }
 // 将“float数据f对应的字符串”写入到“PrintStream输出流”中,print实际调用的是write函数
 public void print(float f) {
  write(String.valueOf(f));
 }
 // 将“double数据d对应的字符串”写入到“PrintStream输出流”中,print实际调用的是write函数
 public void print(double d) {
  write(String.valueOf(d));
 }
 // 将“字符数组s”写入到“PrintStream输出流”中,print实际调用的是write函数
 public void print(char s[]) {
  write(s);
 }
 // 将“字符串数据s”写入到“PrintStream输出流”中,print实际调用的是write函数
 public void print(String s) {
  if (s == null) {
   s = "null";
  }
  write(s);
 }
 // 将“对象obj对应的字符串”写入到“PrintStream输出流”中,print实际调用的是write函数
 public void print(Object obj) {
  write(String.valueOf(obj));
 }
 // 将“换行符”写入到“PrintStream输出流”中,println实际调用的是write函数
 public void println() {
  newLine();
 }
 // 将“boolean数据对应的字符串+换行符”写入到“PrintStream输出流”中,println实际调用的是write函数
 public void println(boolean x) {
  synchronized (this) {
   print(x);
   newLine();
  }
 }
 // 将“字符x对应的字符串+换行符”写入到“PrintStream输出流”中,println实际调用的是write函数
 public void println(char x) {
  synchronized (this) {
   print(x);
   newLine();
  }
 }
 // 将“int数据对应的字符串+换行符”写入到“PrintStream输出流”中,println实际调用的是write函数
 public void println(int x) {
  synchronized (this) {
   print(x);
   newLine();
  }
 }
 // 将“long数据对应的字符串+换行符”写入到“PrintStream输出流”中,println实际调用的是write函数
 public void println(long x) {
  synchronized (this) {
   print(x);
   newLine();
  }
 }
 // 将“float数据对应的字符串+换行符”写入到“PrintStream输出流”中,println实际调用的是write函数
 public void println(float x) {
  synchronized (this) {
   print(x);
   newLine();
  }
 }
 // 将“double数据对应的字符串+换行符”写入到“PrintStream输出流”中,println实际调用的是write函数
 public void println(double x) {
  synchronized (this) {
   print(x);
   newLine();
  }
 }
 // 将“字符数组x+换行符”写入到“PrintStream输出流”中,println实际调用的是write函数
 public void println(char x[]) {
  synchronized (this) {
   print(x);
   newLine();
  }
 }
 // 将“字符串x+换行符”写入到“PrintStream输出流”中,println实际调用的是write函数
 public void println(String x) {
  synchronized (this) {
   print(x);
   newLine();
  }
 }
 // 将“对象o对应的字符串+换行符”写入到“PrintStream输出流”中,println实际调用的是write函数
 public void println(Object x) {
  String s = String.valueOf(x);
  synchronized (this) {
   print(s);
   newLine();
  }
 }
 // 将“数据args”根据“默认Locale值(区域属性)”按照format格式化,并写入到“PrintStream输出流”中
 public PrintStream printf(String format, Object ... args) {
  return format(format, args);
 }
 // 将“数据args”根据“Locale值(区域属性)”按照format格式化,并写入到“PrintStream输出流”中
 public PrintStream printf(Locale l, String format, Object ... args) {
  return format(l, format, args);
 }
 // 根据“默认的Locale值(区域属性)”来格式化数据
 public PrintStream format(String format, Object ... args) {
  try {
   synchronized (this) {
    ensureOpen();
    if ((formatter == null)
     || (formatter.locale() != Locale.getDefault()))
     formatter = new Formatter((Appendable) this);
    formatter.format(Locale.getDefault(), format, args);
   }
  } catch (InterruptedIOException x) {
   Thread.currentThread().interrupt();
  } catch (IOException x) {
   trouble = true;
  }
  return this;
 }
 // 根据“Locale值(区域属性)”来格式化数据
 public PrintStream format(Locale l, String format, Object ... args) {
  try {
   synchronized (this) {
    ensureOpen();
    if ((formatter == null)
     || (formatter.locale() != l))
     formatter = new Formatter(this, l);
    formatter.format(l, format, args);
   }
  } catch (InterruptedIOException x) {
   Thread.currentThread().interrupt();
  } catch (IOException x) {
   trouble = true;
  }
  return this;
 }
 // 将“字符序列的全部字符”追加到“PrintStream输出流中”
 public PrintStream append(CharSequence csq) {
  if (csq == null)
   print("null");
  else
   print(csq.toString());
  return this;
 }
 // 将“字符序列从start(包括)到end(不包括)的全部字符”追加到“PrintStream输出流中”
 public PrintStream append(CharSequence csq, int start, int end) {
  CharSequence cs = (csq == null ? "null" : csq);
  write(cs.subSequence(start, end).toString());
  return this;
 }
 // 将“字符c”追加到“PrintStream输出流中”
 public PrintStream append(char c) {
  print(c);
  return this;
 }
}

说明:

PrintStream的源码比较简单,请上文的注释进行阅读。若有不明白的地方,建议先看看后面的PrintStream使用示例;待搞清它的作用和用法之后,再来阅读源码。

PrintStream和DataOutputStream异同点

相同点:都是继承与FileOutputStream,用于包装其它输出流。

不同点:

(01) PrintStream和DataOutputStream 都可以将数据格式化输出;但它们在“输出字符串”时的编码不同。

       PrintStream是输出时采用的是用户指定的编码(创建PrintStream时指定的),若没有指定,则采用系统默认的字符编码。而DataOutputStream则采用的是UTF-8。

(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
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
import java.io.PrintStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 /**
 * PrintStream 的示例程序
 *
 *
 */
 public class PrintStreamTest {
  public static void main(String[] args) {
   // 下面3个函数的作用都是一样:都是将字母“abcde”写入到文件“file.txt”中。
   // 任选一个执行即可!
   testPrintStreamConstrutor() ;
   //testPrintStreamConstrutor2() ;
  //testPrintStreamConstrutor3() ;
   // 测试write(), print(), println(), printf()等接口。
   testPrintStreamAPIS() ;
  }
  /**
  * PrintStream(OutputStream out) 的测试函数
  *
  * 函数的作用,就是将字母“abcde”写入到文件“file.txt”中
  */
  private static void testPrintStreamConstrutor() {
  // 0x61对应ASCII码的字母'a',0x62对应ASCII码的字母'b', ...
  final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 }; // abced
   try {
    // 创建文件“file.txt”的File对象
    File file = new File("file.txt");
    // 创建文件对应FileOutputStream
    PrintStream out = new PrintStream(
      new FileOutputStream(file));
    // 将“字节数组arr”全部写入到输出流中
    out.write(arr);
    // 关闭输出流
    out.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  /**
  * PrintStream(File file) 的测试函数
  *
  * 函数的作用,就是将字母“abcde”写入到文件“file.txt”中
  */
  private static void testPrintStreamConstrutor2() {
  final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 };
   try {
    File file = new File("file.txt");
    PrintStream out = new PrintStream(file);
    out.write(arr);
    out.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  /**
  * PrintStream(String fileName) 的测试函数
  *
  * 函数的作用,就是将字母“abcde”写入到文件“file.txt”中
  */
  private static void testPrintStreamConstrutor3() {
  final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 };
   try {
    PrintStream out = new PrintStream("file.txt");
    out.write(arr);
    out.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  /**
  * 测试write(), print(), println(), printf()等接口。
  */
  private static void testPrintStreamAPIS() {
   // 0x61对应ASCII码的字母'a',0x62对应ASCII码的字母'b', ...
  final byte[] arr={0x61, 0x62, 0x63, 0x64, 0x65 }; // abced
   try {
    // 创建文件对应FileOutputStream
    PrintStream out = new PrintStream("other.txt");
    // 将字符串“hello PrintStream”+回车符,写入到输出流中
    out.println("hello PrintStream");
    // 将x写入到输出流中
    // x对应ASCII码的字母'A',也就是写入字符'A'
    out.write(0x41);
   // 将字符串"65"写入到输出流中。
   // out.print(0x41); 等价于 out.write(String.valueOf(0x41));
   out.print(0x41);
    // 将字符'B'追加到输出流中
    out.append('B');
   // 将"CDE is 5" + 回车 写入到输出流中
    String str = "CDE";
   int num = 5;
    out.printf("%s is %d\n", str, num);
    out.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

运行上面的代码,会在源码所在目录生成两个文件“file.txt”和“other.txt”。

file.txt的内容如下:

abcde

other.txt的内容如下:

?
1
2
hello PrintStream
A65BCDE is 5

以上所述是小编给大家介绍的Java 中的Printstream知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持

延伸 · 阅读

精彩推荐
  • JAVA教程JavaMail入门教程之发送邮件(3)

    JavaMail入门教程之发送邮件(3)

    这篇文章主要为大家详细介绍了JavaMail入门教程之发送邮件的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 ...

    汪先森4202020-07-07
  • JAVA教程Zookeeper连接超时问题与拒绝连接的解决方案

    Zookeeper连接超时问题与拒绝连接的解决方案

    今天小编就为大家分享一篇关于Zookeeper连接超时问题与拒绝连接的解决方案,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友...

    qq_238762135332019-06-20
  • JAVA教程详解Java编程中JavaMail API的使用

    详解Java编程中JavaMail API的使用

    这篇文章主要介绍了详解Java编程中JavaMail API的使用,通过JavaMail可以实现丰富的邮件类相关功能,需要的朋友可以参考下 ...

    goldensun5062020-03-01
  • JAVA教程再谈java回调函数

    再谈java回调函数

    个人对于回调函数的理解就是回调函数就是回头再调用的函数,哈哈,下面我们来详细探讨下回调函数。 ...

    hebedich4752019-12-30
  • JAVA教程Java集合Iterator迭代的实现方法

    Java集合Iterator迭代的实现方法

    这篇文章主要介绍了Java集合Iterator迭代接口的实现方法,非常不错,具有参考借鉴家,对Java 结合iterator知识感兴趣的朋友一起看看吧 ...

    DemonWang3652020-06-07
  • JAVA教程简单谈谈Java类与类之间的关系

    简单谈谈Java类与类之间的关系

    类与类之间的关系对于理解面向对象具有很重要的作用,以前在面试的时候也经常被问到这个问题,在这里我就简单给大家介绍一下。 ...

    java教程网5202020-05-01
  • JAVA教程java数据结构与算法之快速排序详解

    java数据结构与算法之快速排序详解

    这篇文章主要介绍了java数据结构与算法之快速排序,结合实例形式详细分析了快速排序的原理、实现步骤、相关操作技巧与注意事项,需要的朋友可以参考下...

    android小猪2202020-09-23
  • JAVA教程SpringMvc获取请求头请求体消息过程解析

    SpringMvc获取请求头请求体消息过程解析

    这篇文章主要介绍了SpringMvc获取请求头请求体消息过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    贾树丙1922020-09-18