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();
}
}