改变CSV文件的编码 由gbk改为utf8

/**
 * 改变CSV文件的编码  由gbk改为utf8
 * @author xuchangcheng
 * 2019年9月5日
 *
 */
public class ConversionCSVcoding {
	private final static String SOURCE_ENCODING = "GBK";
	private final static String TARGET_ENCODING = "UTF-8";
	private static String SOURCE = "E:/360/report/chuchong/11.csv";
	private static String TARGET = "E:/360/report/chuchong/1111.csv";

	private static void exchange(String sourcePath, String targetPath) throws IOException {
		FileInputStream fin = null;
		FileOutputStream fout = null;
		FileChannel fcin = null;
		FileChannel fcout = null;
		File source = new File(sourcePath);
		File target = new File(targetPath);
		try {
			fin = new FileInputStream(source);
			fout = new FileOutputStream(target);
			fcin = fin.getChannel();
			fcout = fout.getChannel();
			ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
			while (true) {
				buffer.clear();
				int r = fcin.read(buffer);
				if (r == -1) {
					break;
				}
				buffer.flip();
				String encoding = SOURCE_ENCODING;
				fcout.write(ByteBuffer.wrap(Charset.forName(encoding).decode(buffer).toString().getBytes(TARGET_ENCODING)));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fin != null) {
				fin.close();
			}
			if (fcin != null) {
				fcin.close();
			}
			if (fout != null)
				fout.close();
			if (fcout != null)
				fcout.close();
		}
	}
	
	public static void main(String[] args) {
		try {
			exchange(SOURCE, TARGET);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值