【java基础】关于java使用unicode编码及java编码解码测试数据

测试文件1:UnicodeTest1.java-----该文件自身编码为utf-8

package com.java.unicode;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Arrays;

/**
 * 本java文件编码为UTF-8

 * */
public class UnicodeTest1 {
	public static void main(String[] args) throws Exception{
		
		String GBK_fileName = "E:\\workspace\\TestProgram\\src\\com\\java\\unicode\\GBK.txt";
		String UTF_8_fileName = "E:\\workspace\\TestProgram\\src\\com\\java\\unicode\\UTF-8.txt";
		
		String str = "你是傻逼啊啊啊啊啊啊啊哈哈哈";
		
		System.out.println(codeString(GBK_fileName));
		FileInputStream g_fis = new FileInputStream(GBK_fileName);
		byte[] g_bytes = new byte[50];
		int g_len = g_fis.read(g_bytes);
		
		System.out.println(codeString(UTF_8_fileName));
		FileInputStream u_fis = new FileInputStream(UTF_8_fileName);
		byte[] u_bytes = new byte[50];
		int u_len = u_fis.read(u_bytes);
		byte[] j_bytes_G = str.getBytes("GBK");
		byte[] j_bytes_U = str.getBytes("utf-8");
		//str.getBytes()默认编码方式为当前java文件的编码方式,非系统默认编码
		byte[] j_bytes_F = str.getBytes();
		j_bytes_G = Arrays.copyOf(j_bytes_G, 50);
		j_bytes_U = Arrays.copyOf(j_bytes_U, 50);
		j_bytes_F = Arrays.copyOf(j_bytes_F, 50);
		
		System.out.println("GBK---UTF---J_GBK---J_UTF---J_default");
		//3为utf-8文件文件开始处的utf-8标记,不做文件内容处理
		for (int i = 0 ;i<50-3;i++) {
			System.out.println(g_bytes[i]+"---"+u_bytes[i+3]+"---"+j_bytes_G[i]+"---"+j_bytes_U[i]+"---"+j_bytes_F[i]);
		}
		String a_str = new String(g_bytes,0,g_len,"GBK");
		//new String()默认解码方式问当前java文件的编码方式,非系统默认编码
		String u_str = new String(u_bytes,3,u_len);
		String g_str_j = new String(j_bytes_G,"GBK");
		String u_str_j
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值