java转换流、乱码之编码与解码

来源:互联网 发布:手机歌曲变调软件 编辑:程序博客网 时间:2024/05/16 16:20
  1. package com.bjsxt.io.convert;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4.   
  5. public class ConverDemo01 {  
  6.   
  7.     /** 
  8.      * @param args 
  9.      * @throws UnsupportedEncodingException  
  10.      */  
  11.     public static void main(String[] args) throws UnsupportedEncodingException {  
  12.         String str ="中国";  
  13.         byte[] data =str.getBytes();  
  14.         //字节数不完整  
  15.         System.out.println(new String(data,0,3));  
  16.           
  17.         test1();  
  18.           
  19.     }  
  20.     /** 
  21.      * 编码与解码字符集必须相同,否则乱码 
  22.      * @throws UnsupportedEncodingException  
  23.      */  
  24.     public static void test1() throws UnsupportedEncodingException{  
  25.         //解码 byte -->char  
  26.                 String str ="中国"//gbk   
  27.                 //编码 char -->byte  
  28.                 byte[] data =str.getBytes();  
  29.                 //编码与解码字符集同一  
  30.                 System.out.println(new String(data));  
  31.                 data =str.getBytes("utf-8"); //设定编码字符集  
  32.                 //不同一出现乱码  
  33.                 System.out.println(new String(data));  
  34.                   
  35.                 //编码  
  36.                 byte[] data2 = "中国".getBytes("utf-8");  
  37.                 //解码  
  38.                 str=new String(data2,"utf-8");  
  39.                 System.out.println(str);  
  40.     }  
  41.   
  42. }  


[java] view plain copy
  1. package com.bjsxt.io.convert;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.BufferedReader;  
  6. import java.io.BufferedWriter;  
  7. import java.io.File;  
  8. import java.io.FileInputStream;  
  9. import java.io.FileOutputStream;  
  10. import java.io.IOException;  
  11. import java.io.InputStreamReader;  
  12. import java.io.OutputStreamWriter;  
  13.   
  14. /** 
  15.  * 转换流: 字节转为字符 
  16.  * 1、输出流 OutputStreamWriter 编码 
  17.  * 2、输入流 InputStreamReader  解码 
  18.  *  
  19.  * 确保源不能为乱码 
  20.  * @author Administrator 
  21.  * 
  22.  */  
  23. public class ConverDemo02 {  
  24.   
  25.     /** 
  26.      * @param args 
  27.      * @throws IOException  
  28.      */  
  29.     public static void main(String[] args) throws IOException {  
  30.         //指定解码字符集  
  31.         BufferedReader br =new BufferedReader(  
  32.                 new InputStreamReader(  
  33.                     new BufferedInputStream(  
  34.                             new FileInputStream(   
  35.                                     new File("E:/xp/test/Demo03.java"))),"UTF-8")  
  36.                 );  
  37.           
  38.         // InputStreamReader用于实现字节转换为字符,解码,用utf-8解码  
  39.         //写出文件 编码  
  40.         BufferedWriter bw =new BufferedWriter(  
  41.                 new OutputStreamWriter(  
  42.                     new BufferedOutputStream(     
  43.                     new FileOutputStream(new File("E:/xp/test/encode.java")))));  
  44.                   
  45.         String info =null;  
  46.         while(null!=(info=br.readLine())){  
  47.             //System.out.println(info);  
  48.             bw.write(info);  
  49.             bw.newLine();  
  50.         }  
  51.         bw.flush();  
  52.         bw.close();  
  53.         br.close();  
  54.     }  
  55.   
  56. }  
原创粉丝点击