-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo1.java
80 lines (67 loc) · 2.39 KB
/
demo1.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
package Advances.IOFlow2;
// https://www.youtube.com/watch?v=ITikQNDAYEc&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=599
// https://www.youtube.com/watch?v=JE-Vpvvdzr0&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=600
import java.io.*;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
/**
* IOFlow2 demo1 : Transformation flow : -> transform between 字節流 (byte, 8 bits) & 字符流 (char, 16
* bits)
*
* <p>1) Types: (belongs to 字符流 (char, 16 bits)) InputStreamReader : byte input to char input
* OutputStreamWriter : char output to byte output
*
* <p>2) use cases : byte <-> char
*
* <p>3) decode : byte, byte array -> char, char array encode : char, char array -> byte, byte array
*/
public class demo1 {
/**
* demo 1:
*
* <p>1) Exception handling : we should still use try-catch-finally as standard style 2) demo how
* to use InputStreamReader : byte input -> char input transformation
*/
@Test
public void test1() throws IOException {
FileInputStream fis = new FileInputStream("src/main/java/Advances/IOFlow2/hello.txt");
InputStreamReader isr = new InputStreamReader(fis); // use system default char setting
// InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); // use UTF-8 char type
// explicitly
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1) {
String str = new String(cbuf, 0, len);
System.out.println(str);
}
// close resources
isr.close();
fis.close();
}
/**
* demo 2 : InputStreamReader and OutputStreamWriter
*
* <p>1) Exception handling : we should still use try-catch-finally as standard style
*/
@Test
public void test2() throws IOException {
// step 1) make doc, flow
File file1 = new File("src/main/java/Advances/IOFlow2/hello.txt");
File file2 = new File("src/main/java/Advances/IOFlow2/hello2.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
OutputStreamWriter osw = new OutputStreamWriter(fos, "gbk");
// step 2) read, write data
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1) {
osw.write(cbuf, 0, len);
}
// step 3) close resources
isr.close();
osw.close();
fis.close();
fos.close();
}
}