首页 > 其他 > 详细

字符串编码解析及字符编码输出

时间:2018-07-27 00:39:58      阅读:151      评论:0      收藏:0      [点我收藏+]
package 字符串编码解析;

import java.io.UnsupportedEncodingException;

public class Demo1 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        
        String str = "中国";
        //使用默认编码
        byte[] gbk = str.getBytes("GBK");
        //使用utf-8来编码
        byte[] Utf = str.getBytes("UTF-8");
//        打印编码值
        printByte(gbk);
        System.out.println(new String(gbk));
        //编码必须一致!!!!
//        打印用utf-8编码对应的字符串
        printByte(Utf);
        System.out.println(new String(Utf,"UTF-8"));
    }
    public static void printByte(byte[] bs) {
        for(byte b: bs) {
            System.out.print(b);
        }
    }
}
package 字符编码输出;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class Demo1 {
    public static void main(String[] args) {
        /**
        PrintWriter pw = null;
        try {
             pw = new PrintWriter("1.txt");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(pw != null) {
                pw.close();
                //这里不用try/catch因为在源代码中他自己捕获了自己的异常
            }
        }
        */
        
        
        
        
        /**
         * 写入的编码和读取的编码必须要一致,否则会乱码
         */
        printCharset();
        ReadCharset();
    }
    public static void printCharset() {
        OutputStreamWriter ow = null;
        try {
             ow = new OutputStreamWriter(new FileOutputStream("2.txt"),"GBK");
             ow.write("中国");
             
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(ow != null) {
                try {
                    ow.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
                
    }
    public static void ReadCharset() {
        InputStreamReader ow = null;
        try {
             ow = new InputStreamReader(new FileInputStream("2.txt"),"GBK");
             char[] chs = new char[1024];
             int len = ow.read(chs);
             System.out.println(new String(chs,0,len));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(ow != null) {
                try {
                    ow.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
                
    }
}

 

字符串编码解析及字符编码输出

原文:https://www.cnblogs.com/java-jiangtao-home/p/9374988.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!