最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java字符轉(zhuǎn)碼的三種方法總結(jié)及實(shí)例

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

java字符轉(zhuǎn)碼:三種方法

轉(zhuǎn)碼成功的前提:解碼后無亂碼

轉(zhuǎn)碼流程:文件(gbk)-->解碼-->編碼--->文件(utf-8) 

注:如有問題請留言 

下面具體的實(shí)例

 方法一:Java.lang.String

//用于解碼的構(gòu)造器: 
String(byte[] bytes, int offset, int length, String charsetName)  
String(byte[] bytes, String charsetName)  
 
用于編碼的方法: 
byte[] getBytes(String charsetName) //使用指定字符集進(jìn)行編碼 
 byte[] getBytes() //使用系統(tǒng)默認(rèn)字符集進(jìn)行編碼 

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:橋轉(zhuǎn)換 

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橋轉(zhuǎn)換:對文件進(jìn)行轉(zhuǎn)碼 
 * </pre> 
 * <hr Color="green" ></hr> 
 * 2012 Qingshan Group 版權(quán)所有 
 * <hr Color="green" ></hr> 
 * @author thetopofqingshan 
 * @version 1.0.0 
 * @since  JDK 1.5 
 * @date  2012-4-28 
 */ 
public class CharsetConvertion { 
  private FileInputStream fis;// 文件輸入流:讀取文件中內(nèi)容 
  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:/項(xiàng)目進(jìn)度跟蹤.txt");//文件讀取 
    isr = new InputStreamReader(is, "gbk");//解碼 
    os = new FileOutputStream("C:/項(xiàng)目進(jìn)度跟蹤_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 = "清山集團(tuán)"; 
    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> 
   * 關(guān)閉所有流 
   * </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讀取文件內(nèi)容 
   * </pre> 
   * 
   * @throws IOException 
   *       讀取過程中發(fā)生錯(cuò)誤 
   * 
   */ 
   
  /** 
   * <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轉(zhuǎn)換字符:整個(gè)流程是文件讀取-->byte-->解碼(正確)-->編碼--->byte-->寫入文件 
 * </pre> 
 * <hr Color="green" > 
 * </hr> 
 * 2012 Qingshan Group 版權(quán)所有 
 * <hr Color="green" > 
 * </hr> 
 * 
 * @author thetopofqingshan 
 * @version 1.0.0 
 * @since JDK 1.5 
 * @date 2012-4-27 
 */ 
public class CharsetConvertion { 
  private FileInputStream fis;// 文件輸入流:讀取文件中內(nèi)容 
  private FileChannel in;// 文件通道:雙向,流從中而過 
  private FileChannel out;// 文件通道:雙向,流從中而過 
  private FileOutputStream fos;// 文件輸出流:向文件中寫入內(nèi)容 
  private ByteBuffer b = ByteBuffer.allocate(1024 * 3);// 設(shè)置緩存區(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);// 設(shè)置緩存區(qū)的大小:臨時(shí) 
  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();// 清除標(biāo)記 
      in.read(b); // 將文件內(nèi)容讀入到緩沖區(qū)內(nèi):將標(biāo)記位置從0-b.capacity(), 
            // 讀取完畢標(biāo)記在0-b.capacity()之間 
      b.flip();// 調(diào)節(jié)標(biāo)記,下次讀取從該位置讀起 
      convertion = de.decode(b);// 開始編碼 
 
      temp.clear();// 清除標(biāo)記 
      temp = en.encode(convertion); 
      b.flip(); // 將標(biāo)記移到緩沖區(qū)的開始,并保存其中所有的數(shù)據(jù):將標(biāo)記移到開始0 
      out.write(temp); // 將緩沖區(qū)內(nèi)的內(nèi)容寫入文件中:從標(biāo)記處開始取出數(shù)據(jù) 
    } 
  } 
 
  /** 
   * <pre> 
   * 測試轉(zhuǎn)碼是否成功, 指定字符集讀取文件 
   * </pre> 
   * 
   * @param src 
   *      被復(fù)制文件全路徑 
   * @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> 
   * 關(guān)閉所有流或通道 
   * </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:/項(xiàng)目進(jìn)度跟蹤.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(); 
    } 
  } 
 
} 

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

相關(guān)文章

  • Mybatis-Plus集成Sharding-JDBC與Flyway實(shí)現(xiàn)多租戶分庫分表實(shí)戰(zhàn)

    Mybatis-Plus集成Sharding-JDBC與Flyway實(shí)現(xiàn)多租戶分庫分表實(shí)戰(zhàn)

    這篇文章主要為大家介紹了Mybatis-Plus集成Sharding-JDBC與Flyway實(shí)現(xiàn)多租戶分庫分表實(shí)戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • Java實(shí)現(xiàn)Android拼圖游戲設(shè)計(jì)過程解析

    Java實(shí)現(xiàn)Android拼圖游戲設(shè)計(jì)過程解析

    這篇文章主要介紹了Java實(shí)現(xiàn)Android拼圖游戲設(shè)計(jì)過程解析,下面文章要接受的這是一款基于 Java 開發(fā)的移動端安卓小游戲,可以作為大家在學(xué)習(xí)期間的一個(gè)小練習(xí),接下來和小編一起進(jìn)入文章學(xué)習(xí)具體內(nèi)容吧
    2022-02-02
  • Java中的內(nèi)存泄露問題和解決辦法

    Java中的內(nèi)存泄露問題和解決辦法

    大家好,本篇文章主要講的是Java中的內(nèi)存泄露問題和解決辦法,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • SpringAop中AspectJ框架的切入點(diǎn)表達(dá)式

    SpringAop中AspectJ框架的切入點(diǎn)表達(dá)式

    這篇文章主要介紹了SpringAop中AspectJ框架的切入點(diǎn)表達(dá)式,AspectJ是一個(gè)基于Java語言的AOP框架,Spring2.0以后新增了對AspectJ切點(diǎn)表達(dá)式支持,@AspectJ 是AspectJ1.5新增功能,通過JDK5注解技術(shù),允許直接在Bean類中定義切面,需要的朋友可以參考下
    2023-08-08
  • Springboot中Dependency not found解決方案

    Springboot中Dependency not found解決方案

    本文主要介紹了Springboot中Dependency not found解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11
  • Java經(jīng)典面試題匯總:網(wǎng)絡(luò)編程

    Java經(jīng)典面試題匯總:網(wǎng)絡(luò)編程

    本篇總結(jié)的是Java 網(wǎng)絡(luò)編程相關(guān)的面試題,后續(xù)會持續(xù)更新,希望我的分享可以幫助到正在備戰(zhàn)面試的實(shí)習(xí)生或者已經(jīng)工作的同行,如果發(fā)現(xiàn)錯(cuò)誤還望大家多多包涵,不吝賜教,謝謝
    2021-07-07
  • Java實(shí)例化類詳解

    Java實(shí)例化類詳解

    學(xué)習(xí)JAVA這門面向?qū)ο蟮恼Z言,實(shí)質(zhì)就是不斷地創(chuàng)建類,并把類實(shí)例化為對象并調(diào)用方法。對于初學(xué)JAVA的人總搞清楚對象是如何實(shí)例化的,假如類之間存在繼承關(guān)系,那就更糊涂了。下面我們通過兩個(gè)例題來說明對象的實(shí)例化過程。
    2016-03-03
  • Java中SSM框架實(shí)現(xiàn)增刪改查功能代碼詳解

    Java中SSM框架實(shí)現(xiàn)增刪改查功能代碼詳解

    這篇文章主要介紹了Java中SSM框架實(shí)現(xiàn)增刪改查功能代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 在Java編程中定義方法

    在Java編程中定義方法

    這篇文章主要介紹了在Java編程中定義方法,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-10-10
  • Java編程二項(xiàng)分布的遞歸和非遞歸實(shí)現(xiàn)代碼實(shí)例

    Java編程二項(xiàng)分布的遞歸和非遞歸實(shí)現(xiàn)代碼實(shí)例

    這篇文章主要介紹了Java編程二項(xiàng)分布的遞歸和非遞歸實(shí)現(xiàn)代碼實(shí)例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01

最新評論

肇庆市| 海丰县| 东山县| 石河子市| 利川市| 什邡市| 抚松县| 陆河县| 高州市| 肥城市| 九龙坡区| 藁城市| 莆田市| 丰都县| 通海县| 武清区| 大城县| 虎林市| 巴林右旗| 平湖市| 临猗县| 庄河市| 喜德县| 景洪市| 雷波县| 多伦县| 宣武区| 大化| 那曲县| 辉县市| 闵行区| 石屏县| 珲春市| 宣武区| 寻乌县| 临洮县| 株洲市| 威宁| 田阳县| 依兰县| 保康县|