将GBK格式的文件转为UTF-8格式,避免中文乱码

其实,只要知道源文件的格式,其它格式改一句话,都可以转成UTF-8。都能避免中文乱码。

不用再一行一行的拷贝了



<pre name="code" class="java">import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

public class CharEncodingExchange {
	private final static String SOURCE_ENCODING = "GBK";
	private final static String TARGET_ENCODING = "UTF-8";
	private static String SOURCE_DIR = "/Users/wangzhong/work/recoder/FilterLayoutUtils.java";
	private static String TARGET_DIR = "/Users/wangzhong/work/recoder/FilterLayoutUtils2.java";
	/**
  * @param args
  */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			exchange(SOURCE_DIR);
		} 
		catch (Exception e){
			// TODO Auto-generated catch blockXj
			e.printStackTrace();
			}
		}
	/**
	* exchange the character encoding from srcDir to targetDir
  *
  * @param srcDir
  * @param targetDir
  */
	public static void exchange(String srcDir) {
		String absPath = "";
		if (!srcDir.equals(SOURCE_DIR)) {
			absPath = srcDir.substring(SOURCE_DIR.length());
			String targetDir = TARGET_DIR + absPath;
			File targetDirectory = new File(targetDir);
			if (targetDirectory.isDirectory() && !targetDirectory.exists()) {
				targetDirectory.mkdirs();
			}
		}
		
		File sourceDirectory = new File(srcDir);
		if (sourceDirectory.exists()) {
			if (sourceDirectory.isFile()) {
				String targetFilePath = TARGET_DIR + absPath;
				try {
						fileEncodingExchange(sourceDirectory, targetFilePath);
					} 
				catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} 
			else {
				File[] childs = sourceDirectory.listFiles();
				for (File child : childs)
					exchange(child.getPath());
				}
			}
		}
	
	private static void fileEncodingExchange(File infile,String targetAbsFilePath) throws IOException{ 
		FileInputStream fin = null;
		FileOutputStream fout = null;
		FileChannel fcin = null;
		FileChannel fcout = null;
		System.out.println(infile + " " + targetAbsFilePath);
		String tmpTargetPath = targetAbsFilePath.substring(0, targetAbsFilePath.lastIndexOf(File.separator));
		File tmpTargetDir = new File(tmpTargetPath);
		if (!tmpTargetDir.exists())
			tmpTargetDir.mkdirs();
		try {
			fin = new FileInputStream(infile);
			fout = new FileOutputStream(targetAbsFilePath);
			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; //System.getProperty("file.encoding");
				fcout.write(ByteBuffer.wrap(Charset.forName(encoding).decode(buffer).toString().getBytes(TARGET_ENCODING)));
				}
			} 
		catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				} 
		finally {
					if (fin != null) {
						fin.close();
						}
					if (fcin != null) {
						fcin.close();
						}
					if (fout != null)
						fout.close();
					if (fcout != null)
						fcout.close();
				}
		}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值