编码问题

 

class  EncodingDemo
{
	public static void main(String[] args) throws Exception
	{

		/*
		字符串-->字节数组。编码。
		字节数组-->字符串。解码。
		*/

		String s = "哈哈";

		byte[] b = s.getBytes("gbk");


		
//		printByte(b);
		

		String s1 = new String(b,"UTF-8");

		System.out.println("s1="+s1);


		byte[] b1 = s1.getBytes("UTF-8");

		printByte(b1);

		String s2 = new String(b1,"gbk");

		System.out.println("s2="+s2);
		
	}

	public static void printByte(byte[] arr)
	{
		for(int x=0; x<arr.length; x++)
		{
			System.out.println(arr[x]);
		}
	}
}
/*
ab你好ef谢谢,要求,按照字节个数结束字符串。
如果截取到了半个中文,舍弃。
*/

 

import java.io.*;
class  TransDemo
{
	public static void main(String[] args) throws IOException
	{
		/*
		OutputStreamWriter osw = 
			new OutputStreamWriter(new FileOutputStream("u8.txt"),"utf-8");

		osw.write("你好");

		osw.close();
		*/

		InputStreamReader isr = 
			new InputStreamReader(new FileInputStream("u8.txt"),"gbk");

		char[] buf = new char[10];

		int len = isr.read(buf);

		System.out.println(new String(buf,0,len));

		isr.close();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值