文件(一)--编码问题

几种常见编码如下:

import java.io.UnsupportedEncodingException;


public class CoreEncoder {
	
	public static void main(String[] args) throws Exception {
		String str = "就这样YWD";
		byte[] byte1 = str.getBytes();
		System.out.print("默认GBK :");
		for(byte b:byte1){
			//把字节转换成int以16进制方式表现出来;
			System.out.print(Integer.toHexString(b&0xff)+" ");
		}
		System.out.println();
		byte[] byte2 = str.getBytes("gbk");
		System.out.print("GBK     :");
		for(byte b:byte2){
			//设置编码为gbk输出
			System.out.print(Integer.toHexString(b&0xff)+" ");
		}
		System.out.println();
		byte[] byte3 = str.getBytes("utf-8");
		System.out.print("utf-8   :");
		for(byte b:byte3){
			//设置编码为utf-8输出
			System.out.print(Integer.toHexString(b&0xff)+" ");
		}
		System.out.println();
		byte[] byte4 = str.getBytes("utf-16be");
		System.out.print("utf-16be:");
		for(byte b:byte4){
			//设置编码为gbk输出
			System.out.print(Integer.toHexString(b&0xff)+" ");
		}
		System.out.println();
		String str1 = new String(byte4);//用项目默认编码
		System.out.println(str1);
		String str2 = new String(byte4,"utf-16be");//用对应编码编码
		System.out.println(str2);
	}
	/*
	 * 总结:gbk编码中文两个字节,英文一个;
	 * 		utf-8编码中文三个字节,英文一个;
	 * 		utf-16be中英文都是两个字节;
	 * 当你字节序列是某种编码时,想要将其变成字符串也必须用这种编码;
	 */
}
看以上注释,不用多解释了。

文件内容编码转换

public class IOCVUtils {  
    /** 源文件编码 */  
    public static String sourceEncoding = "GBK";  
    /** 目标编码 */  
    public static String targetEncoding = "UTF-8";  
  
    /**  
     * 文件内容转编码  
     * @param sourceFile  
     * @param targetFile  
     * @throws UnsupportedEncodingException  
     * @throws FileNotFoundException  
     * @throws IOException  
     */  
    public static void changeEncoding(File sourceFile, File targetFile)  
            throws UnsupportedEncodingException, FileNotFoundException,  
            IOException {  
        FileInputStream fin = null;  
        FileOutputStream fout = null;  
        FileChannel fcin = null;  
        FileChannel fcout = null;  
        if (sourceEncoding == null) {  
            IOCVUtils.sourceEncoding = System.getProperty("file.encoding");  
        }  
        try {  
            fin = new FileInputStream(sourceFile);  
            fout = new FileOutputStream(targetFile);  
            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();  
                fcout.write(ByteBuffer.wrap(Charset.forName(sourceEncoding).decode(buffer).toString().getBytes(targetEncoding)));  
            }  
        } finally {  
            if (fin != null) {  
                fin.close();  
                fin = null;  
            }  
            if (fcin != null) {  
                fcin.close();  
                fcin = null;  
            }  
            if (fout != null) {  
                fout.close();  
                fout = null;  
            }  
            if (fcout != null) {  
                fcout.close();  
                fcout = null;  
            }  
        }  
    }  
  
    /**  
     * 文件内容转编码  
     * @param sourceFile  
     * @param targetFile  
     * @throws UnsupportedEncodingException  
     * @throws FileNotFoundException  
     * @throws IOException  
     */  
    public static void changeEncoding(String sourceFile, String targetFile) throws UnsupportedEncodingException, FileNotFoundException, IOException{  
        File fl1 = new File(sourceFile);  
        File fo1 = new File(targetFile);  
        changeEncoding(fl1, fo1);  
    }  
  
    /**  
     * 文件内容转编码  
     * @param sourceFile  
     * @param targetFile  
     * @param sourceEncoding 源文件编码 默认源文件的系统存储编码 System.getProperty("file.encoding");  
     * @param targetEncoding 目标编码 默认utf-8  
     * @throws UnsupportedEncodingException  
     * @throws FileNotFoundException  
     * @throws IOException  
     */  
    public static void changeEncoding(String sourceFile, String targetFile,  
            String sourceEncoding, String targetEncoding) throws UnsupportedEncodingException, FileNotFoundException, IOException {  
        IOCVUtils.sourceEncoding = sourceEncoding;  
        IOCVUtils.targetEncoding = targetEncoding;  
        changeEncoding(sourceFile, targetFile);  
    }  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值