当前位置: 移动技术网 > IT编程>开发语言>Java > java字符转码的三种方法总结及实例

java字符转码的三种方法总结及实例

2019年07月22日  | 移动技术网IT编程  | 我要评论

java字符转码:三种方法

转码成功的前提:解码后无乱码

转码流程:文件(gbk)-->解码-->编码--->文件(utf-8) 

注:如有问题请留言 

下面具体的实例

 方法一:java.lang.string

//用于解码的构造器: 
string(byte[] bytes, int offset, int length, string charsetname)  
string(byte[] bytes, string charsetname)  
 
用于编码的方法: 
byte[] getbytes(string charsetname) //使用指定字符集进行编码 
 byte[] getbytes() //使用系统默认字符集进行编码 

public void convertionstring() throws unsupportedencodingexception{ 
    string s = "清山"; 
    byte[] b = s.getbytes("gbk");//编码 
    string sa = new string(b, "gbk");//解码:用什么字符集编码就用什么字符集解码 
    system.out.println(sa); 
     
    b = sa.getbytes("utf-8");//编码 
    sa = new string(b, "utf-8");//解码 
    system.err.println(sa); 
  } 

方法二:java.io.inputstreamreader/outputstreamwriter:桥转换 

package com.qingshan.io; 
 
import java.io.fileinputstream; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.io.inputstream; 
import java.io.inputstreamreader; 
import java.io.outputstream; 
import java.io.outputstreamwriter; 
import java.io.unsupportedencodingexception; 
 
/** 
 * <pre> 
 * 使用java.io桥转换:对文件进行转码 
 * </pre> 
 * <hr color="green" ></hr> 
 * 2012 qingshan group 版权所有 
 * <hr color="green" ></hr> 
 * @author thetopofqingshan 
 * @version 1.0.0 
 * @since  jdk 1.5 
 * @date  2012-4-28 
 */ 
public class charsetconvertion { 
  private fileinputstream fis;// 文件输入流:读取文件中内容 
  private inputstream is; 
  private inputstreamreader isr; 
  private outputstream os; 
  private outputstreamwriter osw;//写入 
  private char[] ch = new char[1024]; 
  public void convertionfile() throws ioexception{ 
    is = new fileinputstream("c:/项目进度跟踪.txt");//文件读取 
    isr = new inputstreamreader(is, "gbk");//解码 
    os = new fileoutputstream("c:/项目进度跟踪_utf-8.txt");//文件输出 
    osw = new outputstreamwriter(os, "utf-8");//开始编码 
    char[] c = new char[1024];//缓冲 
    int length = 0; 
    while(true){ 
      length = isr.read(c); 
      if(length == -1){ 
        break; 
      } 
      system.out.println(new string(c, 0, length)); 
      osw.write(c, 0, length); 
      osw.flush(); 
    } 
     
  } 
   
  public void convertionstring() throws unsupportedencodingexception{ 
    string s = "清山集团"; 
    byte[] b = s.getbytes("gbk");//编码 
    string sa = new string(b, "gbk");//解码:用什么字符集编码就用什么字符集解码 
    system.out.println(sa); 
     
    b = sa.getbytes("utf-8");//编码 
    sa = new string(b, "utf-8");//解码 
    system.err.println(sa); 
  } 
   
   
 
  /** 
   * <pre> 
   * 关闭所有流 
   * </pre> 
   * 
   */ 
  public void close(){ 
    if(isr != null){ 
      try { 
        isr.close(); 
      } catch (ioexception e) { 
        e.printstacktrace(); 
      } 
    } 
    if(is != null){ 
      try { 
        is.close(); 
      } catch (ioexception e) { 
        // todo auto-generated catch block 
        e.printstacktrace(); 
      } 
    } 
     
    if(osw != null){ 
      try { 
        osw.close(); 
      } catch (ioexception e) { 
        // todo auto-generated catch block 
        e.printstacktrace(); 
      } 
    } 
     
    if(os != null){ 
      try { 
        os.close(); 
      } catch (ioexception e) { 
        // todo auto-generated catch block 
        e.printstacktrace(); 
      } 
    } 
  } 
   
  /** 
   * <pre> 
   * 用io读取文件内容 
   * </pre> 
   * 
   * @throws ioexception 
   *       读取过程中发生错误 
   * 
   */ 
   
  /** 
   * <pre> 
   * 
   * </pre> 
   * @param path 
   * @param charset 
   * @throws ioexception 
   * 
   */ 
  public void read(string path, string charset) throws ioexception { 
    fis = new fileinputstream(path); 
    isr = new inputstreamreader(fis, charset); 
    while (fis.available() > 0) { 
      int length = isr.read(ch);  
      system.out.println(new string(ch)); 
    } 
  } 
 
   
  public static void main(string[] args) { 
    try { 
      charsetconvertion cc = new charsetconvertion(); 
      cc.convertionfile(); 
      cc.convertionstring(); 
      cc.close(); 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } 
  } 
} 

方法三:java.nio.charset

package com.qingshan.nio; 
 
import java.io.fileinputstream; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.io.inputstreamreader; 
import java.nio.bytebuffer; 
import java.nio.charbuffer; 
import java.nio.channels.filechannel; 
import java.nio.charset.charset; 
import java.nio.charset.charsetdecoder; 
import java.nio.charset.charsetencoder; 
 
/** 
 * <pre> 
 * 使用nio中的charset转换字符:整个流程是文件读取-->byte-->解码(正确)-->编码--->byte-->写入文件 
 * </pre> 
 * <hr color="green" > 
 * </hr> 
 * 2012 qingshan group 版权所有 
 * <hr color="green" > 
 * </hr> 
 * 
 * @author thetopofqingshan 
 * @version 1.0.0 
 * @since jdk 1.5 
 * @date 2012-4-27 
 */ 
public class charsetconvertion { 
  private fileinputstream fis;// 文件输入流:读取文件中内容 
  private filechannel in;// 文件通道:双向,流从中而过 
  private filechannel out;// 文件通道:双向,流从中而过 
  private fileoutputstream fos;// 文件输出流:向文件中写入内容 
  private bytebuffer b = bytebuffer.allocate(1024 * 3);// 设置缓存区的大小 
  private charset inset;// 解码字符集 
  private charset outset;// 编码字符集 
  private charsetdecoder de;// 解码器 
  private charsetencoder en;// 编码器 
  private charbuffer convertion;// 中间的字符数据 
  private bytebuffer temp = bytebuffer.allocate(1024 * 3);// 设置缓存区的大小:临时 
  private byte[] by = new byte[1024]; 
  private inputstreamreader isr; 
  private char[] ch = new char[1024]; 
 
  /** 
   * <pre> 
   * 
   * </pre> 
   * 
   * @param src 
   * @param dest 
   * @throws ioexception 
   * 
   */ 
  public void convertionfile_nio(string src, string dest) throws ioexception { 
    fis = new fileinputstream(src); 
    in = fis.getchannel(); 
    fos = new fileoutputstream(dest); 
    out = fos.getchannel(); 
    inset = charset.forname("gbk"); 
    outset = charset.forname("utf-8"); 
    de = inset.newdecoder(); 
    en = outset.newencoder(); 
    while (fis.available() > 0) { 
      b.clear();// 清除标记 
      in.read(b); // 将文件内容读入到缓冲区内:将标记位置从0-b.capacity(), 
            // 读取完毕标记在0-b.capacity()之间 
      b.flip();// 调节标记,下次读取从该位置读起 
      convertion = de.decode(b);// 开始编码 
 
      temp.clear();// 清除标记 
      temp = en.encode(convertion); 
      b.flip(); // 将标记移到缓冲区的开始,并保存其中所有的数据:将标记移到开始0 
      out.write(temp); // 将缓冲区内的内容写入文件中:从标记处开始取出数据 
    } 
  } 
 
  /** 
   * <pre> 
   * 测试转码是否成功, 指定字符集读取文件 
   * </pre> 
   * 
   * @param src 
   *      被复制文件全路径 
   * @param charset 解码字符集 
   * 
   * @throws ioexception 读取过程中的发生的异常 
   * 
   */ 
  public void read(string path, string charset) throws ioexception { 
    fis = new fileinputstream(path); 
    isr = new inputstreamreader(fis, charset); 
    while (fis.available() > 0) { 
      int length = isr.read(ch); 
      system.out.println(new string(ch)); 
    } 
  } 
 
  /** 
   * <pre> 
   * 关闭所有流或通道 
   * </pre> 
   * 
   */ 
  public void close() { 
    try { 
      if (in != null) { 
        in.close(); 
      } 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } 
 
    try { 
      if (out != null) { 
        out.close(); 
      } 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } 
 
    try { 
      if (fis != null) { 
        fis.close(); 
      } 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } 
 
    try { 
      if (fos != null) { 
        fos.close(); 
      } 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } 
  } 
 
  public static void main(string[] args) { 
    charsetconvertion n = new charsetconvertion(); 
    try { 
       n.convertionfile_nio("c:/项目进度跟踪.txt", "c:/nio_write.txt"); 
//     n.read("c:/nio_write.txt", "utf-8");// 正确 
      // n.read("c:/nio_write.txt", "gbk");//乱码 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } finally { 
      n.close(); 
    } 
  } 
 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网