浏览 4391 次
锁定老帖子 主题:自己封装的一个编码转换工具类
精华帖 (0) :: 良好帖 (0) :: 新手帖 (3) :: 隐藏帖 (1)
|
|
---|---|
作者 | 正文 |
发表时间:2009-05-01
java做编码转换有两中方法 1.基于流的编码转换 InputStream is = Main.class.getResourceAsStream(fileName); BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8")); String tmp; while((tmp = br.readLine()) != null){ System.out.println(tmp); }
2.基于字符的编码转换 InputStream is = Main.class.getResourceAsStream(fileName); BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8")); String tmp; while((tmp = br.readLine()) != null){ System.out.println(tmp); }
我这里用的是第一种方法,其他不多说了直接贴代码了
import java.io.ByteArrayInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; public class EncodingConverter { public static final String UTF_8 = "UTF-8"; public static InputStream converter(InputStream is, String srcEnc, String tarEnc) throws IOException { // builder a reader using source encoding Reader reader = new InputStreamReader(is, srcEnc); StringBuilder sb = new StringBuilder(); // read content int c; while ((c = reader.read()) != -1) { sb.append((char) c); } reader.close(); // get byte array InputStream tarIs = new ByteArrayInputStream(sb.toString().getBytes(tarEnc)); return tarIs; } public static InputStream converterToUTF(InputStream is, String srcEnc) throws IOException { // builder a reader using source encoding Reader reader = new InputStreamReader(is, srcEnc); StringBuilder sb = new StringBuilder(); // read content int c; while ((c = reader.read()) != -1) { sb.append((char) c); } reader.close(); // get byte array InputStream tarIs = new ByteArrayInputStream(sb.toString().getBytes("utf-8")); return tarIs; } public static byte[] converterToByteArray(InputStream is, String srcEnc, String tarEnc) throws IOException { // builder a reader using source encoding Reader reader = new InputStreamReader(is, srcEnc); StringBuilder sb = new StringBuilder(); // read content int c; while ((c = reader.read()) != -1) { sb.append((char) c); } reader.close(); // get byte array return sb.toString().getBytes(tarEnc); } public static byte[] converterToUTFByteArray(InputStream is, String srcEnc) throws IOException { // builder a reader using source encoding Reader reader = new InputStreamReader(is, srcEnc); StringBuilder sb = new StringBuilder(); // read content int c; while ((c = reader.read()) != -1) { sb.append((char) c); } reader.close(); // get byte array return sb.toString().getBytes("utf-8"); } public static String converterToString(InputStream is, String srcEnc, String tarEnc) throws IOException { // builder a reader using source encoding Reader reader = new InputStreamReader(is, srcEnc); StringBuilder sb = new StringBuilder(); // read content int c; while ((c = reader.read()) != -1) { sb.append((char) c); } reader.close(); return sb.toString(); } public static String converterToUTFString(InputStream is, String srcEnc) throws IOException { // builder a reader using source encoding Reader reader = new InputStreamReader(is, srcEnc); StringBuilder sb = new StringBuilder(); // read content int c; while ((c = reader.read()) != -1) { sb.append((char) c); } reader.close(); return sb.toString(); } public static InputStream converter(byte[] bytes, String srcEnc, String tarEnc) throws IOException { // builder a reader using source encoding InputStream is = new ByteArrayInputStream(bytes); Reader reader = new InputStreamReader(is, srcEnc); StringBuilder sb = new StringBuilder(); // read content int c; while ((c = reader.read()) != -1) { sb.append((char) c); } reader.close(); // get byte array InputStream tarIs = new ByteArrayInputStream(sb.toString().getBytes(tarEnc)); return tarIs; } public static InputStream converterToUTF(byte[] bytes, String srcEnc) throws IOException { // builder a reader using source encoding InputStream is = new ByteArrayInputStream(bytes); Reader reader = new InputStreamReader(is, srcEnc); StringBuilder sb = new StringBuilder(); // read content int c; while ((c = reader.read()) != -1) { sb.append((char) c); } reader.close(); // get byte array InputStream tarIs = new ByteArrayInputStream(sb.toString().getBytes("utf-8")); return tarIs; } public static byte[] converterToByteArray(byte[] bytes, String srcEnc, String tarEnc) throws IOException { // builder a reader using source encoding InputStream is = new ByteArrayInputStream(bytes); Reader reader = new InputStreamReader(is, srcEnc); StringBuilder sb = new StringBuilder(); // read content int c; while ((c = reader.read()) != -1) { sb.append((char) c); } reader.close(); // get byte array return sb.toString().getBytes(tarEnc); } public static byte[] converterToUTFByteArray(byte[] bytes, String srcEnc) throws IOException { // builder a reader using source encoding InputStream is = new ByteArrayInputStream(bytes); Reader reader = new InputStreamReader(is, srcEnc); StringBuilder sb = new StringBuilder(); // read content int c; while ((c = reader.read()) != -1) { sb.append((char) c); } reader.close(); // get byte array return sb.toString().getBytes("utf-8"); } public static String converterToString(byte[] bytes, String srcEnc, String tarEnc) throws IOException { // builder a reader using source encoding InputStream is = new ByteArrayInputStream(bytes); Reader reader = new InputStreamReader(is, srcEnc); StringBuilder sb = new StringBuilder(); // read content int c; while ((c = reader.read()) != -1) { sb.append((char) c); } reader.close(); return sb.toString(); } public static String converterToUTFString(byte[] bytes, String srcEnc) throws IOException { // builder a reader using source encoding InputStream is = new ByteArrayInputStream(bytes); Reader reader = new InputStreamReader(is, srcEnc); StringBuilder sb = new StringBuilder(); // read content int c; while ((c = reader.read()) != -1) { sb.append((char) c); } reader.close(); return sb.toString(); } public static void main(String[] args) throws IOException { InputStream is = new FileInputStream("G:/out.txt"); is = converter(is, "utf-8", "gbk"); Reader isr = new InputStreamReader(is, "gbk"); StringBuilder sb = new StringBuilder(); int c; while ((c = isr.read()) != -1) { sb.append((char) c); } is.close(); System.out.println(sb.toString()); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-05-02
。。。。。。。。。。。
|
|
返回顶楼 | |