> Java > java지도 시간 > 본문

Java 문자 트랜스코딩을 구현하는 세 가지 방법의 예제 코드 요약

黄舟
풀어 주다: 2017-03-31 10:47:00
원래의
1990명이 탐색했습니다.

이 글에서는 주로 자바 문자 트랜스코딩의 세 가지 방법에 대한 요약과 관련 정보를 예시와 함께 소개하고 있습니다. 도움이 필요한 친구들은

자바 문자 트랜스코딩: 세 가지 방법

을 참고하세요.

성공적인 트랜스코딩을 위한 전제 조건: 디코딩 후 문자가 깨져서는 안 됩니다

트랜스코딩 프로세스: 파일(gbk)-->디코딩--> ;인코딩-- ->파일(utf-8)

참고: 질문이 있는 경우 메시지를 남겨주세요

다음의 구체적인 예

방법 1: Java.lang.String

//用于解码的构造器: 
String(byte[] bytes, int offset, int length, String charsetName)  
String(byte[] bytes, String charsetName)  
 
用于编码的方法: 
byte[] getBytes(String charsetName) //使用指定字符集进行编码 
 byte[] getBytes() //使用系统默认字符集进行编码
로그인 후 복사
rrree

방법 2: java.io.InputStreamReader/ OutputStreamWriter :브리지 변환

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); 
  }
로그인 후 복사

방법 3: java.nio.Charset

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 class="brush:php;toolbar:false"> 
 * 使用java.io桥转换:对文件进行转码 
 * 
*
* 2012 Qingshan Group 版权所有 *
* @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); } /** *
 
   * 关闭所有流 
   * 
* */ 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(); } } } /** *
 
   * 用io读取文件内容 
   * 
* * @throws IOException * 读取过程中发生错误 * */ /** *
 
   * 
   * 
* @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 문자 트랜스코딩을 구현하는 세 가지 방법의 예제 코드 요약의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!