网上介绍:重要的类是InputStreamReader,它是字节转换为字符的桥梁字节流是什么, 字符流是什么,字节转换为字符不是用char() 强制转换就可以了,  为什么要引进这个类呢?

解决方案 »

  1.   

    一个字符是两个字节, 可以说字节比字符低级, 或者说字符是对字节的包装,
    字节流可以读取任何文件, 可以说字符流可以做到的事情, 字节流都可以做到, 反之则不可以. 但字节流效率比较低下.
    给你举个例子, 有一次我们有个需求, 要读取一个pdf文件, 我开始用字符流BufferedReader读取, 结果读出来的文件是乱码, 换成FileInputStream就可以了, 这是因为pdf文件是二进制文件, 那你一个字节一个字节的读取, 会破坏文件. 只能有低级流.
    InputStreamReader是一个适配器, 可以把一个字节流转换为字符流.我们可以这么构造一个InputStreamReader:
    InputStreamReader in = new InputStreamReader(new FileInputStream("c:\\1.pdf"));
      

  2.   

    不要以C/C++的输入输出操作来思考JAVA的输入输出机制.
    JAVA的是流.
      

  3.   

    字节流与字符流主要的区别是他们的的处理对象
    字节流是由字节组成的,字符流是由字符组成的.   
    Java里字符由两个字节组成.字节流是最基本的,所有的InputStrem和OutputStream的子类都是,主要用在处理二进制数据,它是按字节来处理的
    但实际中很多的数据是文本,又提出了字符流的概念,它是按虚拟机的encode来处理,也就是要进行字符集的转化。在从字节流转化为字符流时,实际上就是byte[]转化为String时,
    public String(byte bytes[], String charsetName)
    有一个关键的参数字符集编码,通常我们都省略了,那系统就用操作系统默认的lang
      

  4.   

    下面这个程序用字节流处理,也能处理文件java StreamRecoder  UTF-8 UTF-8 D:\\kk.txt D:\\kk2.txtpackage import java.io.*;
    public class StreamRecoder {
      public static void main(String[] args) {
        if (args.length < 2) {
          System.err.println(
           "Usage: java StreamRecoder "
            + "infile_encoding outfile_encoding infile outfile");
          return;
        }
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
          File infile = new File(args[2]);
          File outfile = new File(args[3]);
          if (outfile.exists( )
            && infile.getCanonicalPath().equals(outfile.getCanonicalPath( ))) {
            System.err.println("Can't convert file in place");
            return;
          }
          FileInputStream fin = new FileInputStream(infile);
          FileOutputStream fout = new FileOutputStream(outfile);
         // isr = new InputStreamReader(fin, args[0]);
         // osw = new OutputStreamWriter(fout, args[1]);
          while (true) {
     
           
            int c = fin.read( );
            if (c == -1) break;  // end of stream
            fout.write(c);
          }
          fout.close( );
          fin.close( );
        }
        catch (IOException ex) {
          System.err.println(ex);
          ex.printStackTrace( );
        }
        finally {
          if (isr != null) {
            try {
              isr.close( );
            } catch (IOException ex) {
              ex.printStackTrace( );
            }
          }
          if (osw != null) {
            try {
              osw.close( );
            }
            catch (IOException ex) {
              ex.printStackTrace( );
            }
          }
        }
      }
    }
    我如果改成这样,用字符流来处理, 如果kk.txt是中文, 就会出现乱码, 
    那用字符流处理还有什么用呢import java.io.*;
    public class StreamRecoder {
      public static void main(String[] args) {
        if (args.length < 2) {
          System.err.println(
           "Usage: java StreamRecoder "
            + "infile_encoding outfile_encoding infile outfile");
          return;
        }
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        try {
          File infile = new File(args[2]);
          File outfile = new File(args[3]);
          if (outfile.exists( )
            && infile.getCanonicalPath().equals(outfile.getCanonicalPath( ))) {
            System.err.println("Can't convert file in place");
            return;
          }
          FileInputStream fin = new FileInputStream(infile);
          FileOutputStream fout = new FileOutputStream(outfile);
          isr = new InputStreamReader(fin, args[0]);
          osw = new OutputStreamWriter(fout, args[1]);
          while (true) {
            int c = isr.read( );
            if (c == -1) break;  // end of stream
            osw.write(c);
          }
          osw.close( );
          isr.close( );
        }
        catch (IOException ex) {
          System.err.println(ex);
          ex.printStackTrace( );
        }
        finally {
          if (isr != null) {
            try {
              isr.close( );
            } catch (IOException ex) {
              ex.printStackTrace( );
            }
          }
          if (osw != null) {
            try {
              osw.close( );
            }
            catch (IOException ex) {
              ex.printStackTrace( );
            }
          }
        }
      }
    }