JAVA 字符串編碼轉換


 

 /** 
  * 字符串編碼轉換的實現方法 
  * @param str  待轉換編碼的字符串 
  * @param newCharset 目標編碼 
  * @return 
  * @throws UnsupportedEncodingException 
  */  
 public String changeCharset(String str, String newCharset)  
   throws UnsupportedEncodingException {  
  if (str != null) {  
   //用默認字符編碼解碼字符串。  
   byte[] bs = str.getBytes();  
   //用新的字符編碼生成字符串  
   return new String(bs, newCharset);  
  }  
  return null;  
 }  
 /** 
  * 字符串編碼轉換的實現方法 
  * @param str  待轉換編碼的字符串 
  * @param oldCharset 原編碼 
  * @param newCharset 目標編碼 
  * @return 
  * @throws UnsupportedEncodingException 
  */  
 public String changeCharset(String str, String oldCharset, String newCharset)  
   throws UnsupportedEncodingException {  
  if (str != null) {  
   //用舊的字符編碼解碼字符串。解碼可能會出現異常。  
   byte[] bs = str.getBytes(oldCharset);  
   //用新的字符編碼生成字符串  
   return new String(bs, newCharset);  
  }  
  return null;  
 }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM