欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java字符轉碼的三種方法總結及實例

 更新時間:2017年03月30日 11:35:47   投稿:lqh  
這篇文章主要介紹了 java字符轉碼的三種方法總結及實例的相關資料,需要的朋友可以參考下

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() //使用系統(tǒng)默認字符集進行編碼 

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 
   *       讀取過程中發(fā)生錯誤 
   * 
   */ 
   
  /** 
   * <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);// 設置緩存區(qū)的大小 
  private Charset inSet;// 解碼字符集 
  private Charset outSet;// 編碼字符集 
  private CharsetDecoder de;// 解碼器 
  private CharsetEncoder en;// 編碼器 
  private CharBuffer convertion;// 中間的字符數(shù)據(jù) 
  private ByteBuffer temp = ByteBuffer.allocate(1024 * 3);// 設置緩存區(qū)的大小:臨時 
  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); // 將文件內容讀入到緩沖區(qū)內:將標記位置從0-b.capacity(), 
            // 讀取完畢標記在0-b.capacity()之間 
      b.flip();// 調節(jié)標記,下次讀取從該位置讀起 
      convertion = de.decode(b);// 開始編碼 
 
      temp.clear();// 清除標記 
      temp = en.encode(convertion); 
      b.flip(); // 將標記移到緩沖區(qū)的開始,并保存其中所有的數(shù)據(jù):將標記移到開始0 
      out.write(temp); // 將緩沖區(qū)內的內容寫入文件中:從標記處開始取出數(shù)據(jù) 
    } 
  } 
 
  /** 
   * <pre> 
   * 測試轉碼是否成功, 指定字符集讀取文件 
   * </pre> 
   * 
   * @param src 
   *      被復制文件全路徑 
   * @param charset 解碼字符集 
   * 
   * @throws IOException 讀取過程中的發(fā)生的異常 
   * 
   */ 
  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(); 
    } 
  } 
 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

  • 2021年最新Redis面試題匯總(3)

    2021年最新Redis面試題匯總(3)

    在程序員面試過程中redis相關的知識是常被問到的話題。這篇文章主要介紹了幾道Redis面試題,整理一下分享給大家,感興趣的小伙伴們可以參考一下
    2021-07-07
  • SpringBoot web開發(fā)源碼深入分析

    SpringBoot web開發(fā)源碼深入分析

    Web開發(fā)的核心內容主要包括內嵌的Servlet容器和SpringMVCSpringBoot使用起來非常簡潔,大部分配置都有SpringBoot自動裝配,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-10-10
  • Spring?Boot整合Kafka教程詳解

    Spring?Boot整合Kafka教程詳解

    這篇文章主要為大家介紹了Spring?Boot整合Kafka教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • java.sql.Date和java.util.Date的區(qū)別詳解

    java.sql.Date和java.util.Date的區(qū)別詳解

    Java中有兩個Date類,一個是java.util.Date通常情況下用它獲取當前時間或構造時間,另一個是java.sql.Date是針對SQL語句使用的,它只包含日期而沒有時間部分,這篇文章主要給大家介紹了關于java.sql.Date和java.util.Date區(qū)別的相關資料,需要的朋友可以參考下
    2023-03-03
  • idea使用Mybatis逆向工程插件詳情

    idea使用Mybatis逆向工程插件詳情

    這篇文章主要介紹了idea使用Mybatis逆向工程插件詳情,首先使用mybatis連接數(shù)據(jù)庫接著添加連接的mysql的信息,測試鏈接等過程,更多過程了解請參考下面文章的詳細內容
    2022-01-01
  • 分析Java設計模式之組合模式

    分析Java設計模式之組合模式

    組合模式是一種對象的行為模式。將對象組合成樹形結構以表示“部分-整體”的層次結構。組合模式使得用戶對單個對象和組合對象的使用具有一致性。它的本質是統(tǒng)一葉子對象和組合對象。它的目的是讓客戶端不再區(qū)分操作的是組合對象還是葉子對象,而是以一個統(tǒng)一的方式來操作
    2021-06-06
  • SpringBoot?@Configuration與@Bean注解使用介紹

    SpringBoot?@Configuration與@Bean注解使用介紹

    這篇文章主要介紹了SpringBoot中的@Configuration與@Bean注解,在進行項目編寫前,我們還需要知道一個東西,就是SpringBoot對我們的SpringMVC還做了哪些配置,包括如何擴展,如何定制,只有把這些都搞清楚了,我們在之后使用才會更加得心應手
    2022-10-10
  • MapStruct實體轉換及List轉換的方法講解

    MapStruct實體轉換及List轉換的方法講解

    今天小編就為大家分享一篇關于MapStruct實體轉換及List轉換的方法講解,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • 解決response.setHeader設置下載文件名無效的問題

    解決response.setHeader設置下載文件名無效的問題

    這篇文章主要介紹了解決response.setHeader設置下載文件名無效的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring?Boot日志SLF4J和Logback示例詳解

    Spring?Boot日志SLF4J和Logback示例詳解

    這篇文章主要介紹了Spring?Boot日志SLF4J和Logback詳解,Logback相比于Log4j,性能提高了10倍以上的性能,占用的內存也變小了,并且文檔十分詳細,推薦使用Slf4j+Logback,需要的朋友可以參考下
    2023-07-07

最新評論