Skip to content

Commit 6639722

Browse files
committed
Add 'AdapterExamples' to 'adapter.example'
1 parent 771aaf3 commit 6639722

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package adapter.example;
2+
3+
import java.io.*;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class AdapterExamples {
8+
9+
public static void main(String[] args) throws IOException {
10+
// 1 - Arrays -> List
11+
List<Integer> list = Arrays.asList(1, 2, 3);
12+
13+
// 2 - InputStreamReader
14+
FileInputStream fis = new FileInputStream("somefile.txt");
15+
InputStreamReader isr = new InputStreamReader(fis);
16+
17+
int t;
18+
while((t = isr.read())!= -1) {
19+
// convert the integer true to character
20+
char r = (char)t;
21+
System.out.println("Character : " + r);
22+
23+
// check if the stream ready
24+
boolean b = isr.ready();
25+
System.out.println("Ready? : " + b);
26+
}
27+
28+
isr.close();
29+
fis.close();
30+
31+
// 3 - OutputStreamWriter
32+
OutputStream os = new FileOutputStream("test.txt");
33+
OutputStreamWriter osw = new OutputStreamWriter(os);
34+
FileInputStream fis2 = new FileInputStream("test.txt");
35+
36+
// use of write(int char):
37+
// writing character values to the "test.txt"
38+
osw.write(71);
39+
osw.write(69);
40+
osw.write(69);
41+
osw.write(75);
42+
osw.write(83);
43+
44+
osw.flush();
45+
46+
// read what we write
47+
for (int i = 0; i < 5; i++) {
48+
// reading the content of "test.txt" file
49+
System.out.println("write(int char): " + (char) fis2.read());
50+
}
51+
osw.close();
52+
}
53+
}

0 commit comments

Comments
 (0)