-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintStreamDemo.java
More file actions
45 lines (38 loc) · 906 Bytes
/
Copy pathPrintStreamDemo.java
File metadata and controls
45 lines (38 loc) · 906 Bytes
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
package io;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
/**
* 打印字节流
*/
public class PrintStreamDemo {
public static void main(String[] args) {
write();
}
public static void write() {
try {
OutputStream out = new FileOutputStream("d:\\test.txt");
// 使用缓冲提供性能
BufferedOutputStream bos = new BufferedOutputStream(out);
// 将缓冲字节输出流转换成打印字节流
PrintStream ps = new PrintStream(bos);
ps.print("Hello PrintStream!\n");
ps.print('a');
ps.println();
ps.println(12);
ps.println(1.18);
ps.println(20d);
ps.println(20l);
ps.close();
bos.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}