java字符集编码是,java字符集与编码有关问题
java字符集與編碼問題
沒想到自己的第一篇javaeye博客就是讓人頭痛的java字符集轉(zhuǎn)碼問題,下面是我個(gè)人的一些認(rèn)識與網(wǎng)上收集的代碼。在java中String在JVM里是unicode的,任何byte[]到String以及String到byte[]都涉及到字符集編碼轉(zhuǎn)換。基本規(guī)則是:
從byte[] 到String就是按某一個(gè)編碼后的字節(jié)數(shù)組轉(zhuǎn)換為unicode的字符串,從String到 byte[]是將unicode的字符串編碼為唯一特定字符集編碼后的字節(jié)數(shù)組。也就是說,Java編譯時(shí)候,會將java文件的編碼按照指定編碼或者系統(tǒng)默認(rèn)的編碼轉(zhuǎn)換為Unicode并加載到內(nèi)存中進(jìn)行編譯。
public String(byte[] bytes)
這個(gè)方法就是完成將bytes[]轉(zhuǎn)碼為unicode的String。
使用的是jvm默認(rèn)的字符集編碼。
如果用戶指定某一個(gè)charsetName,可以是UTF-8,GBK之類的。
public String(byte[] bytes, String charsetName)
兩者都是完成specCharset到unicode的過程,而不是說改變編碼格式為charsetName指定的字符集編碼
同理
public byte[] getBytes()
public byte[] getBytes(String charsetName)
這兩個(gè)方法就是完成從unicode到指定字符集編碼的“轉(zhuǎn)碼”過程。
在瀏覽器中,http請求的parameter到servlet里后,應(yīng)用服務(wù)器已經(jīng)自動(dòng)完成了new String(parameterBytes,browserSpecCharset)這個(gè)過程。
即,自動(dòng)用頁面設(shè)置的charset轉(zhuǎn)碼parameterBytes字節(jié)數(shù)組為unicode的字符串。
下面是網(wǎng)上找的一個(gè)轉(zhuǎn)碼器的例子,經(jīng)測試比較好用,大家可以自己試試
package lavasoft.common;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.*;
/**
* 轉(zhuǎn)碼工具,全面支持文件、字符串的轉(zhuǎn)碼
*
* @author Administrator 2009-11-29 16:14:21
*/
public class EncodingToolkit {
private static Log log = LogFactory.getLog(EncodingToolkit.class);
public static void main(String[] args) {
String han = "漢";
System.out.println("---------");
}
/**
* 對字符串重新編碼
*
* @param text 字符串
* @param resEncoding 源編碼
* @param newEncoding 新編碼
* @return 重新編碼后的字符串
*/
public static String reEncoding(String text, String resEncoding, String newEncoding) {
String rs = null;
try {
rs = new String(text.getBytes(resEncoding), newEncoding);
} catch (UnsupportedEncodingException e) {
log.error("讀取文件為一個(gè)內(nèi)存字符串失敗,失敗原因是使用了不支持的字符編碼");
throw new RuntimeException(e);
}
return rs;
}
/**
* 重新編碼Unicode字符串
*
* @param text 源字符串
* @param newEncoding 新的編碼
* @return 指定編碼的字符串
*/
public static String reEncoding(String text, String newEncoding) {
String rs = null;
try {
rs = new String(text.getBytes(), newEncoding);
} catch (UnsupportedEncodingException e) {
log.error("讀取文件為一個(gè)內(nèi)存字符串失敗,失敗原因是使用了不支持的字符編碼" + newEncoding);
throw new RuntimeException(e);
}
return rs;
}
/**
* 文本文件重新編碼
*
* @param resFile 源文件
* @param resEncoding 源文件編碼
* @param distFile 目標(biāo)文件
* @param newEncoding 目標(biāo)文件編碼
* @return 轉(zhuǎn)碼成功時(shí)候返回ture,否則false
*/
public static boolean reEncoding(File resFile, String resEncoding, File distFile, String newEncoding) {
boolean flag = true;
InputStreamReader reader = null;
OutputStreamWriter writer = null;
try {
reader = new InputStreamReader(new FileInputStream(resFile), resEncoding);
writer = new OutputStreamWriter(new FileOutputStream(distFile), newEncoding);
char buf[] = new char[1024 * 64]; //字符緩沖區(qū)
int len;
while ((len = reader.read(buf)) != -1) {
writer.write(buf, 0, len);
}
writer.flush();
writer.close();
reader.close();
} catch (FileNotFoundException e) {
flag = false;
log.error("沒有找到文件,轉(zhuǎn)碼發(fā)生異常!");
throw new RuntimeException(e);
} catch (IOException e) {
flag = false;
log.error("讀取文件為一個(gè)內(nèi)存字符串失敗,失敗原因是讀取文件異常!");
throw new RuntimeException(e);
} finally {
if (reader != null) try {
reader.close();
} catch (IOException e) {
flag = false;
throw new RuntimeException(e);
} finally {
if (writer != null) try {
writer.close();
} catch (IOException e) {
flag = false;
throw new RuntimeException(e);
}
}
}
return flag;
}
/**
* 讀取文件為一個(gè)Unicode編碼的內(nèi)存字符串,保持文件原有的換行格式
*
* @param resFile 源文件對象
* @param encoding 文件字符集編碼
* @return 文件內(nèi)容的Unicode字符串
*/
public static String file2String(File resFile, String encoding) {
StringBuffer sb = new StringBuffer();
try {
LineNumberReader reader = new LineNumberReader(new BufferedReader(new InputStreamReader(new FileInputStream(resFile), encoding)));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append(System.getProperty("line.separator"));
}
reader.close();
} catch (UnsupportedEncodingException e) {
log.error("讀取文件為一個(gè)內(nèi)存字符串失敗,失敗原因是使用了不支持的字符編碼" + encoding);
throw new RuntimeException(e);
} catch (FileNotFoundException e) {
log.error("讀取文件為一個(gè)內(nèi)存字符串失敗,失敗原因所給的文件" + resFile + "不存在!");
throw new RuntimeException(e);
} catch (IOException e) {
log.error("讀取文件為一個(gè)內(nèi)存字符串失敗,失敗原因是讀取文件異常!");
throw new RuntimeException(e);
}
return sb.toString();
}
/**
* 使用指定編碼讀取輸入流為一個(gè)內(nèi)存Unicode字符串,保持文件原有的換行格式
*
* @param in 輸入流
* @param encoding 構(gòu)建字符流時(shí)候使用的字符編碼
* @return Unicode字符串
*/
public static String stream2String(InputStream in, String encoding) {
StringBuffer sb = new StringBuffer();
LineNumberReader reader = null;
try {
reader = new LineNumberReader(new BufferedReader(new InputStreamReader(in, encoding)));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append(System.getProperty("line.separator"));
}
reader.close();
in.close();
} catch (UnsupportedEncodingException e) {
log.error("讀取文件為一個(gè)內(nèi)存字符串失敗,失敗原因是使用了不支持的字符編碼" + encoding);
throw new RuntimeException(e);
} catch (IOException e) {
log.error("讀取文件為一個(gè)內(nèi)存字符串失敗,失敗原因是讀取文件異常!");
throw new RuntimeException(e);
} finally {
if (in != null) try {
in.close();
} catch (IOException e) {
log.error("關(guān)閉輸入流發(fā)生異常!", e);
throw new RuntimeException(e);
}
}
return sb.toString();
}
/**
* 字符串保存為制定編碼的文本文件
*
* @param text 字符串
* @param distFile 目標(biāo)文件
* @param encoding 目標(biāo)文件的編碼
* @return 轉(zhuǎn)換成功時(shí)候返回ture,否則false
*/
public static boolean string2TextFile(String text, File distFile, String encoding) {
boolean flag = true;
if (!distFile.getParentFile().exists()) distFile.getParentFile().mkdirs();
OutputStreamWriter writer = null;
try {
writer = new OutputStreamWriter(new FileOutputStream(distFile), encoding);
writer.write(text);
writer.close();
} catch (IOException e) {
flag = false;
log.error("將字符串寫入文件發(fā)生異常!");
throw new RuntimeException(e);
} finally {
if (writer != null) try {
writer.close();
} catch (IOException e) {
log.error("關(guān)閉輸出流發(fā)生異常!", e);
throw new RuntimeException(e);
}
}
return flag;
}
}
1 樓
jyjava
2011-12-29
流為啥要關(guān)閉兩次
總結(jié)
以上是生活随笔為你收集整理的java字符集编码是,java字符集与编码有关问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php cpu mac,PHP 获得计算
- 下一篇: oracle导数卡死,oracle-审计