String编码问题
我想测试下编码的头,所以,写了些东西,突然,发现点不知所以的问题,前来求教下:
假定"F:/test.txt"中有两个汉字“你好”
public class Test {
private static byte w[] = new byte[1024];
public void readInt() throws IOException{//按照int来读取
System.out.print("Integer info:\t");
FileInputStream fileInS = new FileInputStream("F:/test.txt");//文件中是“你好”
int w = fileInS.read() ;
while( -1 != w ){
System.out.print(w+" ");
w = fileInS.read();
}
System.out.println();
}
public void readByteArray() throws IOException{
System.out.print("ByteArray info: \t");
FileInputStream fileInS = new FileInputStream("F:/test.txt");//文件中是“你好”
while( -1 != fileInS.read(w) ){
String tmp = toHexDecimal(w);
System.out.print(tmp);
}
System.out.println();
}
private String toHexDecimal(byte... decimal){//将byte[]转型为16进制
byte[] refDecimal = decimal;
StringBuffer sBuf = new StringBuffer();
for( int i=0; i<refDecimal.length ; i++ ){
sBuf.append(Integer.toHexString(refDecimal[i])).append(" ");
}
return sBuf.toString() ;
}
Integer info:228 189 160 229 165 189
ByteArray info: ffffffe4 ffffffbd ffffffa0 ffffffe5 ffffffa5