java IO 之InputStreamReader和OutputStreamWriter
1 package com.io.test;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6
7 import org.junit.Test;
8
9 public class TestInputStreamReader {
10
11 @Test
12 public void test() {
13 InputStreamReader reader = new InputStreamReader(System.in);
14 BufferedReader br = new BufferedReader(reader);
15 String s = null;
16 try {
17 s = br.readLine();
18 while (s != null) {
19 if (s.equalsIgnoreCase("exit"))
20 break;
21 System.out.println(s.toUpperCase());
22 s = br.readLine();
23 }
24 br.close();
25 } catch (IOException e) {
26 e.printStackTrace();
27 }
28
29 }
30 }
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6
7 import org.junit.Test;
8
9 public class TestInputStreamReader {
10
11 @Test
12 public void test() {
13 InputStreamReader reader = new InputStreamReader(System.in);
14 BufferedReader br = new BufferedReader(reader);
15 String s = null;
16 try {
17 s = br.readLine();
18 while (s != null) {
19 if (s.equalsIgnoreCase("exit"))
20 break;
21 System.out.println(s.toUpperCase());
22 s = br.readLine();
23 }
24 br.close();
25 } catch (IOException e) {
26 e.printStackTrace();
27 }
28
29 }
30 }
1 package com.io.test;
2
3 import java.io.FileNotFoundException;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.OutputStreamWriter;
7
8 import org.junit.Test;
9
10 public class TestOutputStreamWriter {
11
12 @Test
13 public void test() {
14 try {
15 OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("/Users/Jet/Documents/workspace/IOTest/test/com/io/test/test.java"));
16 writer.write("开始输入字符:");
17 System.out.println(writer.getEncoding());
18 writer.close();
19
20 writer = new OutputStreamWriter(new FileOutputStream("/Users/Jet/Documents/workspace/IOTest/test/com/io/test/test.java",true),"ISO-8859-1");
21 writer.write("又一次输入字符");
22 System.out.println(writer.getEncoding());
23 writer.close();
24
25 } catch (FileNotFoundException e) {
26 e.printStackTrace();
27 } catch (IOException e) {
28 e.printStackTrace();
29 }
30 }
31
32 }
2
3 import java.io.FileNotFoundException;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.OutputStreamWriter;
7
8 import org.junit.Test;
9
10 public class TestOutputStreamWriter {
11
12 @Test
13 public void test() {
14 try {
15 OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("/Users/Jet/Documents/workspace/IOTest/test/com/io/test/test.java"));
16 writer.write("开始输入字符:");
17 System.out.println(writer.getEncoding());
18 writer.close();
19
20 writer = new OutputStreamWriter(new FileOutputStream("/Users/Jet/Documents/workspace/IOTest/test/com/io/test/test.java",true),"ISO-8859-1");
21 writer.write("又一次输入字符");
22 System.out.println(writer.getEncoding());
23 writer.close();
24
25 } catch (FileNotFoundException e) {
26 e.printStackTrace();
27 } catch (IOException e) {
28 e.printStackTrace();
29 }
30 }
31
32 }