怎么使用InputStreamReader

本文通过一个具体的Java程序示例,对比了使用字节流与字符流处理文件的不同效果,特别是针对中文文件处理时可能出现的问题。通过示例说明了InputStreamReader和OutputStreamWriter在处理字符编码转换中的作用。
部署运行你感兴趣的模型镜像

网上介绍:重要的类是InputStreamReader,它是字节转换为字符的桥梁

字节流是什么, 字符流是什么,字节转换为字符不是用char() 强制转换就可以了,  为什么要引进这个类呢?


下面这个程序用字节流处理,也能处理文件

java StreamRecoder  UTF-8 UTF-8 D://kk.txt D://kk2.txt

package

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( );
        }
      }
    }
  }
}

您可能感兴趣的与本文相关的镜像

Seed-Coder-8B-Base

Seed-Coder-8B-Base

文本生成
Seed-Coder

Seed-Coder是一个功能强大、透明、参数高效的 8B 级开源代码模型系列,包括基础变体、指导变体和推理变体,由字节团队开源

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值