I/O流之--转换流:InputStreamReader 和InputStreamWriter

I/O流之--转换流:InputStreamReader 和InputStreamWriter

分类: java
 

目录(?)[+]

 
 

一、InputStreamReader类

InputStreamReader 将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。

构造方法:

InputStreamReader isr = new InputStreamReader(InputStream in);//构造一个默认编码集的InputStreamReader类

InputStreamReader isr = new InputStreamReader(InputStream in,String charsetName);//构造一个指定编码集的InputStreamReader类。

参数 in对象通过 InputStream in = System.in;获得。//读取键盘上的数据。

                                或者    InputStream in = new FileInputStream(String fileName);//读取文件中的数据。可以看出FileInputStream 为InputStream的子类。

主要方法:int read();//读取单个字符。

                  int read(char []cbuf);//将读取到的字符存到数组中。返回读取的字符数。

[java] view plaincopy
 
  1. public static void transReadNoBuf() throws IOException {  
  2.         /** 
  3.          * 没有缓冲区,只能使用read()方法。 
  4.          */  
  5.         //读取字节流  
  6. //      InputStream in = System.in;//读取键盘的输入。  
  7.         InputStream in = new FileInputStream("D:\demo.txt");//读取文件的数据。  
  8.         //将字节流向字符流的转换。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节.  
  9.         InputStreamReader isr = new InputStreamReader(in);//读取  
  10. //      InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\demo.txt"));//综合到一句。  
  11.               
  12.         char []cha = new char[1024];  
  13.         int len = isr.read(cha);  
  14.         System.out.println(new String(cha,0,len));  
  15.         isr.close();  
  16.   
  17.     }  
  18.     public static void transReadByBuf() throws IOException {  
  19.         /** 
  20.          * 使用缓冲区 可以使用缓冲区对象的 read() 和  readLine()方法。 
  21.          */  
  22.         //读取字节流  
  23. //      InputStream in = System.in;//读取键盘上的数据  
  24.         InputStream in = new FileInputStream("D:\demo.txt");//读取文件上的数据。  
  25.         //将字节流向字符流的转换。  
  26.         InputStreamReader isr = new InputStreamReader(in);//读取  
  27.         //创建字符流缓冲区  
  28.         BufferedReader bufr = new BufferedReader(isr);//缓冲  
  29. //      BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("D:\demo.txt")));可以综合到一句。  
  30.     /*  int ch =0; 
  31.         ch = bufr.read(); 
  32.         System.out.println((char)ch);*/  
  33.         String line = null;  
  34.         while((line = bufr.readLine())!=null){  
  35.             System.out.println(line);  
  36.         }  
  37.         isr.close();  
  38.     }  
[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. public static void transReadNoBuf() throws IOException {  
  2.         /** 
  3.          * 没有缓冲区,只能使用read()方法。 
  4.          */  
  5.         //读取字节流  
  6. //      InputStream in = System.in;//读取键盘的输入。  
  7.         InputStream in = new FileInputStream("D:\demo.txt");//读取文件的数据。  
  8.         //将字节流向字符流的转换。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节.  
  9.         InputStreamReader isr = new InputStreamReader(in);//读取  
  10. //      InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\demo.txt"));//综合到一句。  
  11.               
  12.         char []cha = new char[1024];  
  13.         int len = isr.read(cha);  
  14.         System.out.println(new String(cha,0,len));  
  15.         isr.close();  
  16.   
  17.     }  
  18.     public static void transReadByBuf() throws IOException {  
  19.         /** 
  20.          * 使用缓冲区 可以使用缓冲区对象的 read() 和  readLine()方法。 
  21.          */  
  22.         //读取字节流  
  23. //      InputStream in = System.in;//读取键盘上的数据  
  24.         InputStream in = new FileInputStream("D:\demo.txt");//读取文件上的数据。  
  25.         //将字节流向字符流的转换。  
  26.         InputStreamReader isr = new InputStreamReader(in);//读取  
  27.         //创建字符流缓冲区  
  28.         BufferedReader bufr = new BufferedReader(isr);//缓冲  
  29. //      BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("D:\demo.txt")));可以综合到一句。  
  30.     /*  int ch =0; 
  31.         ch = bufr.read(); 
  32.         System.out.println((char)ch);*/  
  33.         String line = null;  
  34.         while((line = bufr.readLine())!=null){  
  35.             System.out.println(line);  
  36.         }  
  37.         isr.close();  
  38.     }  

 

二、OutputStreamWriter类

OutputStreamWriter 将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。

构造方法:

OutputStreamWriter osw = new OutputStreamWriter(OutputStream out);//构造一个默认编码集的OutputStreamWriter类

OutputStreamWriter osw = new OutputStreamWriter(OutputStream out,String charsetName);//构造一个指定编码集的OutputStreamWriter类。

参数 out对象通过 InputStream out = System.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 plaincopy
 
  1.     public static void transWriteNoBuf() throws IOException {  
  2.         OutputStream out = System.out;//打印到控制台  
  3. //      OutputStream out = new FileOutputStream("D:\demo.txt");//打印到文件  
  4.         OutputStreamWriter osr = new OutputStreamWriter(out);//输出  
  5. //      OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\demo.txt"));//两句可以综合到一句。  
  6. //      int ch = 97;//a  
  7. //      int ch = 20320;//你  
  8. //      osr.write(ch);  
  9.         String str = "你好吗?";//你好吗?  
  10.         osr.write(str);  
  11.         osr.flush();  
  12.         osr.close();  
  13.     }  
  14.     public static void transWriteByBuf() throws IOException {  
  15. //      OutputStream out = System.out;//打印到控制台。  
  16.         OutputStream out = new FileOutputStream("D:\demo.txt");//打印到文件。  
  17.         OutputStreamWriter osr = new OutputStreamWriter(out);//输出  
  18. //      OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\demo.txt"));//综合到一句。  
  19.         BufferedWriter bufw = new BufferedWriter(osr);//缓冲  
  20. //      int ch = 97;//a  
  21. //      int ch = 20320;//你  
  22. //      osr.write(ch);  
  23.         String str = "你好吗? 我很好!";//你好吗?  
  24.         bufw.write(str);  
  25.         bufw.flush();  
  26.         bufw.close();  
  27.     }  
[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1.     public static void transWriteNoBuf() throws IOException {  
  2.         OutputStream out = System.out;//打印到控制台  
  3. //      OutputStream out = new FileOutputStream("D:\demo.txt");//打印到文件  
  4.         OutputStreamWriter osr = new OutputStreamWriter(out);//输出  
  5. //      OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\demo.txt"));//两句可以综合到一句。  
  6. //      int ch = 97;//a  
  7. //      int ch = 20320;//你  
  8. //      osr.write(ch);  
  9.         String str = "你好吗?";//你好吗?  
  10.         osr.write(str);  
  11.         osr.flush();  
  12.         osr.close();  
  13.     }  
  14.     public static void transWriteByBuf() throws IOException {  
  15. //      OutputStream out = System.out;//打印到控制台。  
  16.         OutputStream out = new FileOutputStream("D:\demo.txt");//打印到文件。  
  17.         OutputStreamWriter osr = new OutputStreamWriter(out);//输出  
  18. //      OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\demo.txt"));//综合到一句。  
  19.         BufferedWriter bufw = new BufferedWriter(osr);//缓冲  
  20. //      int ch = 97;//a  
  21. //      int ch = 20320;//你  
  22. //      osr.write(ch);  
  23.         String str = "你好吗? 我很好!";//你好吗?  
  24.         bufw.write(str);  
  25.         bufw.flush();  
  26.         bufw.close();  
  27.     }  

 

 流转换程序1:

[java] view plaincopy
 
  1. package IOtest;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.InputStreamReader;  
  10. import java.io.OutputStream;  
  11. import java.io.OutputStreamWriter;  
  12.   
  13. public class TransStreamtest {  
  14.   
  15.     /** 
  16.      * 主要的类:    in1,    InputStream 
  17.      *                      创建对象 InputStream in = System.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 = System.out; 
  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 = System.out;//打印到控制台  
  48. //      OutputStream out = new FileOutputStream("D:\demo.txt");//打印到文件  
  49.         OutputStreamWriter osr = new OutputStreamWriter(out);//输出  
  50. //      OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\demo.txt"));//两句可以综合到一句。  
  51. //      int ch = 97;//a  
  52. //      int ch = 20320;//你  
  53. //      osr.write(ch);  
  54.         String str = "你好吗?";//你好吗?  
  55.         osr.write(str);  
  56.         osr.flush();  
  57.         osr.close();  
  58.     }  
  59.         public static void transWriteByBuf() throws IOException {  
  60. //          OutputStream out = System.out;//打印到控制台。  
  61.             OutputStream out = new FileOutputStream("D:\demo.txt");//打印到文件。  
  62.             OutputStreamWriter osr = new OutputStreamWriter(out);//输出  
  63. //          OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\demo.txt"));//综合到一句。  
  64.             BufferedWriter bufw = new BufferedWriter(osr);//缓冲  
  65. //      int ch = 97;//a  
  66. //      int ch = 20320;//你  
  67. //      osr.write(ch);  
  68.             String str = "你好吗? 我很好!";//你好吗?  
  69.             bufw.write(str);  
  70.             bufw.flush();  
  71.             bufw.close();  
  72.     }  
  73.   
  74.       
  75.     public static void transReadNoBuf() throws IOException {  
  76.         /** 
  77.          * 没有缓冲区,只能使用read()方法。 
  78.          */  
  79.         //读取字节流  
  80. //      InputStream in = System.in;//读取键盘的输入。  
  81.         InputStream in = new FileInputStream("D:\demo.txt");//读取文件的数据。  
  82.         //将字节流向字符流的转换。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节.  
  83.         InputStreamReader isr = new InputStreamReader(in);//读取  
  84. //      InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\demo.txt"));//综合到一句。  
  85.               
  86.         char []cha = new char[1024];  
  87.         int len = isr.read(cha);  
  88.         System.out.println(new String(cha,0,len));  
  89.         isr.close();  
  90.     }  
  91.       
  92.     public static void transReadByBuf() throws IOException {  
  93.         /** 
  94.          * 使用缓冲区 可以使用缓冲区对象的 read() 和  readLine()方法。 
  95.          */  
  96.         //读取字节流  
  97. //      InputStream in = System.in;//读取键盘上的数据  
  98.         InputStream in = new FileInputStream("D:\demo.txt");//读取文件上的数据。  
  99.         //将字节流向字符流的转换。  
  100.         InputStreamReader isr = new InputStreamReader(in);//读取  
  101.         //创建字符流缓冲区  
  102.         BufferedReader bufr = new BufferedReader(isr);//缓冲  
  103. //      BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("D:\demo.txt")));可以综合到一句。  
  104.     /*  int ch =0; 
  105.         ch = bufr.read(); 
  106.         System.out.println((char)ch);*/  
  107.         String line = null;  
  108.         while((line = bufr.readLine())!=null){  
  109.             System.out.println(line);  
  110.         }  
  111.         isr.close();  
  112.     }  
  113. }  
[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package IOtest;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.InputStreamReader;  
  10. import java.io.OutputStream;  
  11. import java.io.OutputStreamWriter;  
  12.   
  13. public class TransStreamtest {  
  14.   
  15.     /** 
  16.      * 主要的类:    in1,    InputStream 
  17.      *                      创建对象 InputStream in = System.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 = System.out; 
  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 = System.out;//打印到控制台  
  48. //      OutputStream out = new FileOutputStream("D:\demo.txt");//打印到文件  
  49.         OutputStreamWriter osr = new OutputStreamWriter(out);//输出  
  50. //      OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\demo.txt"));//两句可以综合到一句。  
  51. //      int ch = 97;//a  
  52. //      int ch = 20320;//你  
  53. //      osr.write(ch);  
  54.         String str = "你好吗?";//你好吗?  
  55.         osr.write(str);  
  56.         osr.flush();  
  57.         osr.close();  
  58.     }  
  59.         public static void transWriteByBuf() throws IOException {  
  60. //          OutputStream out = System.out;//打印到控制台。  
  61.             OutputStream out = new FileOutputStream("D:\demo.txt");//打印到文件。  
  62.             OutputStreamWriter osr = new OutputStreamWriter(out);//输出  
  63. //          OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream("D:\demo.txt"));//综合到一句。  
  64.             BufferedWriter bufw = new BufferedWriter(osr);//缓冲  
  65. //      int ch = 97;//a  
  66. //      int ch = 20320;//你  
  67. //      osr.write(ch);  
  68.             String str = "你好吗? 我很好!";//你好吗?  
  69.             bufw.write(str);  
  70.             bufw.flush();  
  71.             bufw.close();  
  72.     }  
  73.   
  74.       
  75.     public static void transReadNoBuf() throws IOException {  
  76.         /** 
  77.          * 没有缓冲区,只能使用read()方法。 
  78.          */  
  79.         //读取字节流  
  80. //      InputStream in = System.in;//读取键盘的输入。  
  81.         InputStream in = new FileInputStream("D:\demo.txt");//读取文件的数据。  
  82.         //将字节流向字符流的转换。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节.  
  83.         InputStreamReader isr = new InputStreamReader(in);//读取  
  84. //      InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\demo.txt"));//综合到一句。  
  85.               
  86.         char []cha = new char[1024];  
  87.         int len = isr.read(cha);  
  88.         System.out.println(new String(cha,0,len));  
  89.         isr.close();  
  90.     }  
  91.       
  92.     public static void transReadByBuf() throws IOException {  
  93.         /** 
  94.          * 使用缓冲区 可以使用缓冲区对象的 read() 和  readLine()方法。 
  95.          */  
  96.         //读取字节流  
  97. //      InputStream in = System.in;//读取键盘上的数据  
  98.         InputStream in = new FileInputStream("D:\demo.txt");//读取文件上的数据。  
  99.         //将字节流向字符流的转换。  
  100.         InputStreamReader isr = new InputStreamReader(in);//读取  
  101.         //创建字符流缓冲区  
  102.         BufferedReader bufr = new BufferedReader(isr);//缓冲  
  103. //      BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("D:\demo.txt")));可以综合到一句。  
  104.     /*  int ch =0; 
  105.         ch = bufr.read(); 
  106.         System.out.println((char)ch);*/  
  107.         String line = null;  
  108.         while((line = bufr.readLine())!=null){  
  109.             System.out.println(line);  
  110.         }  
  111.         isr.close();  
  112.     }  
  113. }  

 流转换程序2:

[java] view plaincopy
 
  1. package readKey;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.FileReader;  
  6. import java.io.FileWriter;  
  7. import java.io.IOException;  
  8. import java.io.InputStreamReader;  
  9. import java.io.OutputStreamWriter;  
  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.txt"),"UTF-8");  
  30.         char []ch = new char[20];  
  31.         int len = isr.read(ch);  
  32.         System.out.println(new String(ch,0,len) );  
  33.         isr.close();      
  34.           
  35.     }  
  36.     public static void ReadTest_2() throws IOException {  
  37.         InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\utf-8.txt"),"GBK");  
  38.         char []ch = new char[20];  
  39.         int len = isr.read(ch);  
  40.         System.out.println(new String(ch,0,len) );  
  41.         isr.close();      
  42.           
  43.     }  
  44.     public static void ReadTest_1() throws IOException {  
  45.         FileReader fr = new FileReader("D:\demo.txt");  
  46.         char []ch = new char[20];  
  47.         int len = fr.read(ch);  
  48.         System.out.println(new String(ch,0,len) );  
  49.         fr.close();       
  50.     }  
  51.   
  52.   
  53.     public static void writeText_3() throws IOException {  
  54.         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\utf-8.txt"),"UTF-8");  
  55.         osw.write("你好吗");  
  56.         osw.close();      
  57.     }  
  58.   
  59.     public static void writeText_2() throws IOException {  
  60.         FileWriter fw = new FileWriter("D:\gbk1.txt");  
  61.         fw.write("你好啊");  
  62.         fw.close();  
  63.     }  
  64.   
  65.     public static void writeText_1() throws IOException {  
  66.         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\gbk.txt"),"GBK");  
  67.         /* 
  68.          *和上面的等同  
  69.          * FileWriter fw = new FileWriter("D:\gbk1.txt"); 
  70.          * 操作文件的字节流 + 默认的编码表 
  71.          */  
  72.         osw.write("你好吗");  
  73.         osw.close();  
  74.     }  
  75. }  
原文地址:https://www.cnblogs.com/wzhanke/p/4792775.html