java字符转码:三种方法

来源:互联网 发布:迷彩服淘宝分类 编辑:程序博客网 时间:2024/06/08 13:16

原文地址:http://thetopofqingshan.iteye.com/blog/1502731

java字符转码:三种方法

转码成功的前提:解码后无乱码
转码流程:文件(gbk)-->解码-->编码--->文件(utf-8)

 

注:如有问题请留言

 

下面具体的实例

 

方法一:Java.lang.String

 

Java代码  收藏代码
  1. 用于解码的构造器:  
  2. String(byte[] bytes, int offset, int length, String charsetName)   
  3. String(byte[] bytes, String charsetName)   
  4.   
  5. 用于编码的方法:  
  6. byte[] getBytes(String charsetName)  //使用指定字符集进行编码  
  7.  byte[] getBytes() //使用系统默认字符集进行编码  
  8.    

 

Java.lang.string转码代码  收藏代码
  1. public void convertionString() throws UnsupportedEncodingException{  
  2.         String s = "清山";  
  3.         byte[] b = s.getBytes("gbk");//编码  
  4.         String sa = new String(b, "gbk");//解码:用什么字符集编码就用什么字符集解码  
  5.         System.out.println(sa);  
  6.           
  7.         b = sa.getBytes("utf-8");//编码  
  8.         sa = new String(b, "utf-8");//解码  
  9.         System.err.println(sa);  
  10.     }  

方法二:java.io.InputStreamReader/OutputStreamWriter:桥转换

 

Java代码  收藏代码
  1. package com.qingshan.io;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.InputStreamReader;  
  8. import java.io.OutputStream;  
  9. import java.io.OutputStreamWriter;  
  10. import java.io.UnsupportedEncodingException;  
  11.   
  12. /** 
  13.  * <pre> 
  14.  * 使用java.io桥转换:对文件进行转码 
  15.  * </pre> 
  16.  * <hr Color="green" ></hr>  
  17.  * 2012 Qingshan Group 版权所有 
  18.  * <hr Color="green" ></hr>  
  19.  * @author  thetopofqingshan 
  20.  * @version 1.0.0 
  21.  * @since   JDK 1.5 
  22.  * @date    2012-4-28 
  23.  */  
  24. public class CharsetConvertion {  
  25.     private FileInputStream fis;// 文件输入流:读取文件中内容  
  26.     private InputStream is;  
  27.     private InputStreamReader isr;  
  28.     private OutputStream os;  
  29.     private OutputStreamWriter osw;//写入  
  30.     private char[] ch = new char[1024];  
  31.     public  void convertionFile() throws IOException{  
  32.         is = new FileInputStream("C:/项目进度跟踪.txt");//文件读取  
  33.         isr = new InputStreamReader(is, "gbk");//解码  
  34.         os = new FileOutputStream("C:/项目进度跟踪_utf-8.txt");//文件输出  
  35.         osw = new OutputStreamWriter(os, "utf-8");//开始编码  
  36.         char[] c = new char[1024];//缓冲  
  37.         int length = 0;  
  38.         while(true){  
  39.             length = isr.read(c);  
  40.             if(length == -1){  
  41.                 break;  
  42.             }  
  43.             System.out.println(new String(c, 0, length));  
  44.             osw.write(c, 0, length);  
  45.             osw.flush();  
  46.         }  
  47.           
  48.     }  
  49.       
  50.     public void convertionString() throws UnsupportedEncodingException{  
  51.         String s = "清山集团";  
  52.         byte[] b = s.getBytes("gbk");//编码  
  53.         String sa = new String(b, "gbk");//解码:用什么字符集编码就用什么字符集解码  
  54.         System.out.println(sa);  
  55.           
  56.         b = sa.getBytes("utf-8");//编码  
  57.         sa = new String(b, "utf-8");//解码  
  58.         System.err.println(sa);  
  59.     }  
  60.       
  61.       
  62.   
  63.     /** 
  64.      * <pre> 
  65.      * 关闭所有流 
  66.      * </pre> 
  67.      * 
  68.      */  
  69.     public void close(){  
  70.         if(isr != null){  
  71.             try {  
  72.                 isr.close();  
  73.             } catch (IOException e) {  
  74.                 e.printStackTrace();  
  75.             }  
  76.         }  
  77.         if(is != null){  
  78.             try {  
  79.                 is.close();  
  80.             } catch (IOException e) {  
  81.                 // TODO Auto-generated catch block  
  82.                 e.printStackTrace();  
  83.             }  
  84.         }  
  85.           
  86.         if(osw != null){  
  87.             try {  
  88.                 osw.close();  
  89.             } catch (IOException e) {  
  90.                 // TODO Auto-generated catch block  
  91.                 e.printStackTrace();  
  92.             }  
  93.         }  
  94.           
  95.         if(os != null){  
  96.             try {  
  97.                 os.close();  
  98.             } catch (IOException e) {  
  99.                 // TODO Auto-generated catch block  
  100.                 e.printStackTrace();  
  101.             }  
  102.         }  
  103.     }  
  104.       
  105.     /** 
  106.      * <pre> 
  107.      * 用io读取文件内容 
  108.      * </pre> 
  109.      *  
  110.      * @throws IOException 
  111.      *             读取过程中发生错误 
  112.      *  
  113.      */  
  114.       
  115.     /** 
  116.      * <pre> 
  117.      *  
  118.      * </pre> 
  119.      * @param path 
  120.      * @param charset 
  121.      * @throws IOException 
  122.      * 
  123.      */  
  124.     public void read(String path, String charset) throws IOException {  
  125.         fis = new FileInputStream(path);  
  126.         isr = new InputStreamReader(fis, charset);  
  127.         while (fis.available() > 0) {  
  128.             int length = isr.read(ch);   
  129.             System.out.println(new String(ch));  
  130.         }  
  131.     }  
  132.   
  133.       
  134.     public static void main(String[] args) {  
  135.         try {  
  136.             CharsetConvertion cc = new CharsetConvertion();  
  137.             cc.convertionFile();  
  138.             cc.convertionString();  
  139.             cc.close();  
  140.         } catch (IOException e) {  
  141.             e.printStackTrace();  
  142.         }  
  143.     }  
  144. }  
 

 

方法三:java.nio.Charset

 

Java代码  收藏代码
  1. package com.qingshan.nio;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStreamReader;  
  7. import java.nio.ByteBuffer;  
  8. import java.nio.CharBuffer;  
  9. import java.nio.channels.FileChannel;  
  10. import java.nio.charset.Charset;  
  11. import java.nio.charset.CharsetDecoder;  
  12. import java.nio.charset.CharsetEncoder;  
  13.   
  14. /** 
  15.  * <pre> 
  16.  * 使用nio中的Charset转换字符:整个流程是文件读取-->byte-->解码(正确)-->编码--->byte-->写入文件 
  17.  * </pre> 
  18.  * <hr Color="green" > 
  19.  * </hr> 
  20.  * 2012 Qingshan Group 版权所有 
  21.  * <hr Color="green" > 
  22.  * </hr> 
  23.  *  
  24.  * @author thetopofqingshan 
  25.  * @version 1.0.0 
  26.  * @since JDK 1.5 
  27.  * @date 2012-4-27 
  28.  */  
  29. public class CharsetConvertion {  
  30.     private FileInputStream fis;// 文件输入流:读取文件中内容  
  31.     private FileChannel in;// 文件通道:双向,流从中而过  
  32.     private FileChannel out;// 文件通道:双向,流从中而过  
  33.     private FileOutputStream fos;// 文件输出流:向文件中写入内容  
  34.     private ByteBuffer b = ByteBuffer.allocate(1024 * 3);// 设置缓存区的大小  
  35.     private Charset inSet;// 解码字符集  
  36.     private Charset outSet;// 编码字符集  
  37.     private CharsetDecoder de;// 解码器  
  38.     private CharsetEncoder en;// 编码器  
  39.     private CharBuffer convertion;// 中间的字符数据  
  40.     private ByteBuffer temp = ByteBuffer.allocate(1024 * 3);// 设置缓存区的大小:临时  
  41.     private byte[] by = new byte[1024];  
  42.     private InputStreamReader isr;  
  43.     private char[] ch = new char[1024];  
  44.   
  45.     /** 
  46.      * <pre> 
  47.      *  
  48.      * </pre> 
  49.      *  
  50.      * @param src 
  51.      * @param dest 
  52.      * @throws IOException 
  53.      *  
  54.      */  
  55.     public void convertionFile_nio(String src, String dest) throws IOException {  
  56.         fis = new FileInputStream(src);  
  57.         in = fis.getChannel();  
  58.         fos = new FileOutputStream(dest);  
  59.         out = fos.getChannel();  
  60.         inSet = Charset.forName("gbk");  
  61.         outSet = Charset.forName("utf-8");  
  62.         de = inSet.newDecoder();  
  63.         en = outSet.newEncoder();  
  64.         while (fis.available() > 0) {  
  65.             b.clear();// 清除标记  
  66.             in.read(b); // 将文件内容读入到缓冲区内:将标记位置从0-b.capacity(),  
  67.                         // 读取完毕标记在0-b.capacity()之间  
  68.             b.flip();// 调节标记,下次读取从该位置读起  
  69.             convertion = de.decode(b);// 开始编码  
  70.   
  71.             temp.clear();// 清除标记  
  72.             temp = en.encode(convertion);  
  73.             b.flip(); // 将标记移到缓冲区的开始,并保存其中所有的数据:将标记移到开始0  
  74.             out.write(temp); // 将缓冲区内的内容写入文件中:从标记处开始取出数据  
  75.         }  
  76.     }  
  77.   
  78.     /** 
  79.      * <pre> 
  80.      * 测试转码是否成功, 指定字符集读取文件 
  81.      * </pre> 
  82.      *  
  83.      * @param src 
  84.      *            被复制文件全路径 
  85.      * @param charset 解码字符集 
  86.      *  
  87.      * @throws IOException 读取过程中的发生的异常 
  88.      *  
  89.      */  
  90.     public void read(String path, String charset) throws IOException {  
  91.         fis = new FileInputStream(path);  
  92.         isr = new InputStreamReader(fis, charset);  
  93.         while (fis.available() > 0) {  
  94.             int length = isr.read(ch);  
  95.             System.out.println(new String(ch));  
  96.         }  
  97.     }  
  98.   
  99.     /** 
  100.      * <pre> 
  101.      * 关闭所有流或通道 
  102.      * </pre> 
  103.      *  
  104.      */  
  105.     public void close() {  
  106.         try {  
  107.             if (in != null) {  
  108.                 in.close();  
  109.             }  
  110.         } catch (IOException e) {  
  111.             e.printStackTrace();  
  112.         }  
  113.   
  114.         try {  
  115.             if (out != null) {  
  116.                 out.close();  
  117.             }  
  118.         } catch (IOException e) {  
  119.             e.printStackTrace();  
  120.         }  
  121.   
  122.         try {  
  123.             if (fis != null) {  
  124.                 fis.close();  
  125.             }  
  126.         } catch (IOException e) {  
  127.             e.printStackTrace();  
  128.         }  
  129.   
  130.         try {  
  131.             if (fos != null) {  
  132.                 fos.close();  
  133.             }  
  134.         } catch (IOException e) {  
  135.             e.printStackTrace();  
  136.         }  
  137.     }  
  138.   
  139.     public static void main(String[] args) {  
  140.         CharsetConvertion n = new CharsetConvertion();  
  141.         try {  
  142.              n.convertionFile_nio("C:/项目进度跟踪.txt""C:/nio_write.txt");  
  143. //          n.read("C:/nio_write.txt", "utf-8");// 正确  
  144.             // n.read("C:/nio_write.txt", "gbk");//乱码  
  145.         } catch (IOException e) {  
  146.             e.printStackTrace();  
  147.         } finally {  
  148.             n.close();  
  149.         }  
  150.     }  
  151.   

原创粉丝点击