自己封装的一个编码转换工具类

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());
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值