zoukankan      html  css  js  c++  java
  • 用字节流解决GBK编码UTF-8编码的转换

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class BianMaDemo4 {
        public static void main(String[] args) throws IOException, FileNotFoundException {
            
            /*
             * 
             * GBK编码字节流与UTF-8编码字节流的转换:
             * 操作步骤就是:先解码:new String(src,0,len,"GBK")得到字符串;再使用getBytes("UTF-8")得到UTF-8编码字节数组        
             */
            
            //gbk-->utf-8
            FileInputStream fis=new FileInputStream("gbk_file.txt");//gbk文件
            FileOutputStream fos=new FileOutputStream("222.txt");//utf-8文件
            byte[] src=new byte[1024];
            int len;
            byte[] dest;
            while((len=fis.read(src))!=-1){
                dest=new String(src,0,len,"GBK").getBytes("UTF-8");
                fos.write(dest,0,dest.length );
            }
            
            
            //utf-8-->gbk
            /*FileInputStream fis=new FileInputStream("222.txt");//utf-8文件
            FileOutputStream fos=new FileOutputStream("gbk_file.txt");//gbk文件
            byte[] src=new byte[1024];
            int len;
            byte[] dest;
            while((len=fis.read(src))!=-1){
                dest=new String(src,0,len,"UTF-8").getBytes("GBK");
                fos.write(dest);
            }*/
    
            fis.close();//释放资源
            fos.close();
            
        }
    
    }
  • 相关阅读:
    MT【319】分段递推数列
    MT【318】分式不等式双代换
    Centos7环境变量
    VI快捷键
    Centos7 开机自动运行命令
    Centos7 编辑本地DNS解析配置文件
    Centos7修改主机名
    xadmin 自定义过滤器选项
    Centos7网卡配置文件
    Centos7 挂载
  • 原文地址:https://www.cnblogs.com/abtious/p/12293387.html
Copyright © 2011-2022 走看看