java字符集與編碼問題
沒想到自己的第一篇javaeye博客就是讓人頭痛的java字符集轉碼問題,下面是我個人的一些認識與網上收集的代碼。在java中String在JVM里是unicode的,任何byte[]到String以及String到byte[]都涉及到字符集編碼轉換。基本規則是:
從byte[] 到String就是按某一個編碼后的字節數組轉換為unicode的字符串,從String到 byte[]是將unicode的字符串編碼為唯一特定字符集編碼后的字節數組。也就是說,Java編譯時候,會將java文件的編碼按照指定編碼或者系統默認的編碼轉換為Unicode并加載到內存中進行編譯。
public String(byte[] bytes)
這個方法就是完成將bytes[]轉碼為unicode的String。
使用的是jvm默認的字符集編碼。
如果用戶指定某一個charsetName,可以是UTF-8,GBK之類的。
public String(byte[] bytes, String charsetName)
兩者都是完成specCharset到unicode的過程,而不是說改變編碼格式為charsetName指定的字符集編碼
同理
public byte[] getBytes()
public byte[] getBytes(String charsetName)
這兩個方法就是完成從unicode到指定字符集編碼的“轉碼”過程。
在瀏覽器中,http請求的parameter到servlet里后,應用服務器已經自動完成了new String(parameterBytes,browserSpecCharset)這個過程。
即,自動用頁面設置的charset轉碼parameterBytes字節數組為unicode的字符串。
下面是網上找的一個轉碼器的例子,經測試比較好用,大家可以自己試試
package lavasoft.common;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.*;
/**
* 轉碼工具,全面支持文件、字符串的轉碼
*
* @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("讀取文件為一個內存字符串失敗,失敗原因是使用了不支持的字符編碼");
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("讀取文件為一個內存字符串失敗,失敗原因是使用了不支持的字符編碼" + newEncoding);
throw new RuntimeException(e);
}
return rs;
}
/**
* 文本文件重新編碼
*
* @param resFile 源文件
* @param resEncoding 源文件編碼
* @param distFile 目標文件
* @param newEncoding 目標文件編碼
* @return 轉碼成功時候返回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]; //字符緩沖區
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("沒有找到文件,轉碼發生異常!");
throw new RuntimeException(e);
} catch (IOException e) {
flag = false;
log.error("讀取文件為一個內存字符串失敗,失敗原因是讀取文件異常!");
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;
}
/**
* 讀取文件為一個Unicode編碼的內存字符串,保持文件原有的換行格式
*
* @param resFile 源文件對象
* @param encoding 文件字符集編碼
* @return 文件內容的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("讀取文件為一個內存字符串失敗,失敗原因是使用了不支持的字符編碼" + encoding);
throw new RuntimeException(e);
} catch (FileNotFoundException e) {
log.error("讀取文件為一個內存字符串失敗,失敗原因所給的文件" + resFile + "不存在!");
throw new RuntimeException(e);
} catch (IOException e) {
log.error("讀取文件為一個內存字符串失敗,失敗原因是讀取文件異常!");
throw new RuntimeException(e);
}
return sb.toString();
}
/**
* 使用指定編碼讀取輸入流為一個內存Unicode字符串,保持文件原有的換行格式
*
* @param in 輸入流
* @param encoding 構建字符流時候使用的字符編碼
* @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("讀取文件為一個內存字符串失敗,失敗原因是使用了不支持的字符編碼" + encoding);
throw new RuntimeException(e);
} catch (IOException e) {
log.error("讀取文件為一個內存字符串失敗,失敗原因是讀取文件異常!");
throw new RuntimeException(e);
} finally {
if (in != null) try {
in.close();
} catch (IOException e) {
log.error("關閉輸入流發生異常!", e);
throw new RuntimeException(e);
}
}
return sb.toString();
}
/**
* 字符串保存為制定編碼的文本文件
*
* @param text 字符串
* @param distFile 目標文件
* @param encoding 目標文件的編碼
* @return 轉換成功時候返回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("將字符串寫入文件發生異常!");
throw new RuntimeException(e);
} finally {
if (writer != null) try {
writer.close();
} catch (IOException e) {
log.error("關閉輸出流發生異常!", e);
throw new RuntimeException(e);
}
}
return flag;
}
}
1 樓
jyjava
2011-12-29
流為啥要關閉兩次