Java字符集编码与转码
Java字符的class代码总是Unicode字符集的UTF-16编码,运行时内存中的字符串在没有指定编码的时候也总是Unicode编码。
Java编译时候,会将java文件的编码按照指定编码或者(系统默认的)编码转换为Unicode并加载到内存中进行编译。
下面给出一个Java转码工具,没有测试过,呵呵:
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;
}
}
分享到:
相关推荐
Java字符集和编码 ,Java字符集和编码Java字符集和编码
java字符集编码乱码详解
字符集编码问题字符集编码问题字符集编码问题
java 字符集编码转换,时间格式化,数字判断等,java文件
NULL 博文链接:https://joard.iteye.com/blog/403031
Java中的字符集编码入门(五)Java代码中的字符编码转换Part1.pdf
java 获取文件字符集编码依赖包
Java字符集编码应用探讨
而由字符组成的集合则成为字符集,字符集由于包含字符的多少与异同而形成了各种不同的字符集。我们知道,所有字符在计算机中都是以二进制来存储的。那么一个字符究竟由多少个二进制位来表示呢?这就涉及到字符编码的...
JAVA及相关字符集编码问题,相当经典。
java字符串编码转换和web中的字符串转换
字符集与字符集编码简介 我们知道,计算机只能识别诸如0101这样的二进制数,于是人们必须以二进制数据与计算机进行交互,或者先将人类使用的字符按一定规则转换为二进制数。 那什么是字符呢?在计算机领域,我们把...
Java中的字符集编码入门(二)编码字符集与字符集编码的区别[参考].pdf
通用的文件字符编码集判断需要借助第三方包cpdetector.jar 使用Cpdetector jar包检测文件编码需要依赖antlr-2.7.7.jar、chardet-1.0.jar、jargs-1.0.jar三个jar包 本下载资源一站式全包含,并附带亲测有效的片段...
java字符串的各种编码转换. java字符串的各种编码转换
为了方便大家在jsp页面中,通过js代码实现字符集转换,在这儿总结了一些js字符集转码函数,方便大家使用!!
字符集编码查询/反查工具
qt实现转换文件字符集编码
java_字符编码.txt Javajava_字符编码问题
java连接AmericanascII7字符集oracle例子