码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-07-27 01:31:44      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:默认   fileinput   print   null   demo   col   code   gen   imp   

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

 

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

标签:默认   fileinput   print   null   demo   col   code   gen   imp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!