2024年2月22日发(作者:)
InputStreamReader 和 OutputStreamWriter类用法简介。
一、InputStreamReader类
InputStreamReader 将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。
构造方法:
InputStreamReader isr = new InputStreamReader(InputStream in);//构造一个默认编码集的InputStreamReader类
InputStreamReader isr = new InputStreamReader(InputStream in,String charsetName);//构造一个指定编码集的InputStreamReader类。
参数 in对象通过 InputStream in = ;获得。//读取键盘上的数据。
或者 InputStream in = new FileInputStream(String fileName);//读取文件中的数据。可以看出FileInputStream 为InputStream的子类。
主要方法:int read();//读取单个字符。
int read(char []cbuf);//将读取到的字符存到数组中。返回读取的字符数。
[java] view plaincopyprint?
1. public static void transReadNoBuf() throws IOException {
2. /**
3. * 没有缓冲区,只能使用read()方法。
4. */
5. //读取字节流
6. // InputStream in = ;//读取键盘的输入。
7. InputStream in = new FileInputStream("D:");//读取文件的数据。
8. //将字节流向字符流的转换。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节.
9. InputStreamReader isr = new InputStreamReader(in);//读取
10. // InputStreamReader isr = new InputStreamReader(new FileInputStream("D:"));//综合到一句。
11.
12. char []cha = new char[1024];
13. int len = (cha);
14. n(new String(cha,0,len));
15. ();
16.
17. }
18. public static void transReadByBuf() throws IOException {
19. /**
20. * 使用缓冲区 可以使用缓冲区对象的 read() 和 readLine()方法。
21. */
22. //读取字节流
23. // InputStream in = ;//读取键盘上的数据
24. InputStream in = new FileInputStream("D:");//读取文件上的数据。
25. //将字节流向字符流的转换。
26. InputStreamReader isr = new InputStreamReader(in);//读取
27. //创建字符流缓冲区
28. BufferedReader bufr = new BufferedReader(isr);//缓冲
29. // BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("D:")));可以综合到一句。
30. /* int ch =0;
31. ch = ();
32. n((char)ch);*/
33. String line = null;
34. while((line = ne())!=null){
35. n(line);
36. }
37. ();
38. }
二、OutputStreamWriter类
OutputStreamWriter 将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。
构造方法:
OutputStreamWriter osw = new OutputStreamWriter(OutputStream out);//构造一个默认编码集的OutputStreamWriter类
OutputStreamWriter osw = new OutputStreamWriter(OutputStream out,String charsetName);//构造一个指定编码集的OutputStreamWriter类。
参数 out对象通过 InputStream out = ;获得。//打印到控制台上。
或者 InputStream out = new FileoutputStream(String fileName);//输出到文件中。可以看出FileoutputStream 为outputStream的子类。
主要方法:void write(int c);//将单个字符写入。
viod write(String str,int off,int len);//将字符串某部分写入。
void flush();//将该流中的缓冲数据刷到目的地中去。
[java] view plaincopyprint?
1. public static void transWriteNoBuf() throws IOException {
2. OutputStream out = ;//打印到控制台
3. // OutputStream out = new FileOutputStream("D:");//打印到文件
4. OutputStreamWriter osr = new OutputStreamWriter(out);//输出
5. // OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:"));//两句可以综合到一句。
6. // int ch = 97;//a
7. // int ch = 20320;//你
8. // (ch);
9. String str = "你好吗?";//你好吗?
10. (str);
11. ();
12. ();
13. }
14. public static void transWriteByBuf() throws IOException {
15. // OutputStream out = ;//打印到控制台。
16. OutputStream out = new FileOutputStream("D:");//打印到文件。
17. OutputStreamWriter osr = new OutputStreamWriter(out);//输出
18. // OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:"));//综合到一句。
19. BufferedWriter bufw = new BufferedWriter(osr);//缓冲
20. // int ch = 97;//a
21. // int ch = 20320;//你
22. // (ch);
23. String str = "你好吗?rn我很好!";//你好吗?
24. (str);
25. ();
26. ();
27. }
流转换程序1:
[java] view plaincopyprint?
1. package IOtest;
2.
3. import edReader;
4. import edWriter;
5. import putStream;
6. import tputStream;
7. import ption;
8. import tream;
9. import treamReader;
10. import Stream;
11. import StreamWriter;
12.
13. public class TransStreamtest {
14.
15. /**
16. * 主要的类: in1, InputStream
17. * 创建对象 InputStream in = ;
18. * in2, InputStreamReader 没有readLine()方法
19. * 主要方法:
20. * read()读取单个字符,一个汉字也为一个字符。
21. * read(char[] cbuf)将字符读入数组。
22. * close().关闭此流和相关联资源。
23. * in3, BufferedReader 有read(),readLine()方法。
24. * out1, OutputStream
25. * 创建对象 OutputStream in = ;
26. * out2, OutputStreamWriter
27. * 主要方法:
28. * write(int c)//写入单个字符。
29. * write(char[] cbuf,int off,int len)//写入数组的某一部分
30. * write(String str,int off,int len)//写入字符串烦人某一部分。
31. * flush();//刷新该流中的缓冲。
32. * close();
33. * out3, BufferedWriteer 有Write(int ch),newLine()方法。
34. *
35. *
36. * @throws IOException
37. */
38. public static void main(String[] args) throws IOException {
39. // transReadByBuf();
40. // transReadNoBuf();
41. transWriteNoBuf();
42. // transWriteByBuf();
43.
44. }
45.
46. public static void transWriteNoBuf() throws IOException {
47. OutputStream out = ;//打印到控制台
48. // OutputStream out = new FileOutputStream("D:");//打印到文件
49. OutputStreamWriter osr = new OutputStreamWriter(out);//输出
50. // OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:"));//两句可以综合到一句。
51. // int ch = 97;//a
52. // int ch = 20320;//你
53. // (ch);
54. String str = "你好吗?";//你好吗?
55. (str);
56. ();
57. ();
58. }
59. public static void transWriteByBuf() throws IOException {
60. // OutputStream out = ;//打印到控制台。
61. OutputStream out = new FileOutputStream("D:");//打印到文件。
62. OutputStreamWriter osr = new OutputStreamWriter(out);//输出
63. // OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:"));//综合到一句。
64. BufferedWriter bufw = new BufferedWriter(osr);//缓冲
65. // int ch = 97;//a
66. // int ch = 20320;//你
67. // (ch);
68. String str = "你好吗?rn我很好!";//你好吗?
69. (str);
70. ();
71. ();
72. }
73.
74.
75. public static void transReadNoBuf() throws IOException {
76. /**
77. * 没有缓冲区,只能使用read()方法。
78. */
79. //读取字节流
80. // InputStream in = ;//读取键盘的输入。
81. InputStream in = new FileInputStream("D:");//读取文件的数据。
82. //将字节流向字符流的转换。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节.
83. InputStreamReader isr = new InputStreamReader(in);//读取
84. // InputStreamReader isr = new InputStreamReader(new FileInputStream("D:"));//综合到一句。
85.
86. char []cha = new char[1024];
87. int len = (cha);
88. n(new String(cha,0,len));
89. ();
90. }
91.
92. public static void transReadByBuf() throws IOException {
93. /**
94. * 使用缓冲区 可以使用缓冲区对象的 read() 和 readLine()方法。
95. */
96. //读取字节流
97. // InputStream in = ;//读取键盘上的数据
98. InputStream in = new FileInputStream("D:");//读取文件上的数据。
99. //将字节流向字符流的转换。
100. InputStreamReader isr = new InputStreamReader(in);//读取
101. //创建字符流缓冲区
102. BufferedReader bufr = new BufferedReader(isr);//缓冲
103. // BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("D:")));可以综合到一句。
104. /* int ch =0;
105. ch = ();
106. n((char)ch);*/
107. String line = null;
108. while((line = ne())!=null){
109. n(line);
110. }
111. ();
112. }
113. }
流转换程序2:
[java] view plaincopyprint?
1. package readKey;
2.
3. import putStream;
4. import tputStream;
5. import ader;
6. import iter;
7. import ption;
8. import treamReader;
9. import StreamWriter;
10.
11. public class TransStreamDemo3 {
12.
13. /**
14. * @param args
15. * @throws IOException
16. */
17. public static void main(String[] args) throws IOException {
18. // writeText_1();
19. // writeText_2();
20. // writeText_3();
21. // ReadTest_1();
22. // ReadTest_2();
23. // ReadTest_3();
24.
25. }
26.
27.
28. public static void ReadTest_3() throws IOException {
29. InputStreamReader isr = new InputStreamReader(new FileInputStream("D:"),"UTF-8");
30. char []ch = new char[20];
31. int len = (ch);
32. n(new String(ch,0,len) );
33. ();
34.
35. }
36. public static void ReadTest_2() throws IOException {
37. InputStreamReader isr = new InputStreamReader(new FileInputStream("D:"),"GBK");
38. char []ch = new char[20];
39. int len = (ch);
40. n(new String(ch,0,len) );
41. ();
42.
43. }
44. public static void ReadTest_1() throws IOException {
45. FileReader fr = new FileReader("D:");
46. char []ch = new char[20];
47. int len = (ch);
48. n(new String(ch,0,len) );
49. ();
50. }
51.
52.
53. public static void writeText_3() throws IOException {
54. OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:"),"UTF-8");
55. ("你好吗");
56. ();
57. }
58.
59. public static void writeText_2() throws IOException {
60. FileWriter fw = new FileWriter("D:");
61. ("你好啊");
62. ();
63. }
64.
65. public static void writeText_1() throws IOException {
66. OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:"),"GBK");
67. /*
68. *和上面的等同
69. * FileWriter fw = new FileWriter("D:");
70. * 操作文件的字节流 + 默认的编码表
71. */
72. ("你好吗");
73. ();
74. }
75. }
发布评论