String(byte[] ascii, int hibyte, int offset, int count)
Deprecated. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the String constructors that take a charset name or that use the platform's default charset.
String(byte[] bytes, int offset, int length, String charsetName)
Constructs a new String by decoding the specified subarray of bytes using the specified charset.
Deprecated. This method does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via the String constructors that take a charset name or that use the platform's default charset.
String(byte[] bytes, int offset, int length, String charsetName)
Constructs a new String by decoding the specified subarray of bytes using the specified charset.
是ISO8859-1,我要把这个字符串存到数据库里去,我的数据库采用的是Shift_JIS码。
于是我发现存进去后原来好端端的字符串都变成了"???",我就要转码。于是调用
String str2 = String(str1.getBytes("ISO8859-1"), 0, str1.length, "Shift_JIS");
第2个参数表示我从第几个byte开始转,第3个参数表示要转多少个byte。
够清楚了吗?
如果用String s = new String(b,0,2,"gb2312");则返回"你",第2-3个参数说明返回从开头(0)开始的2各字节的解码(一个汉字两个字节);用String s = new String(b,"iso8859-1");则返回乱码,因为加密字符集与解密字符集不一致。
FileInputStream fileInput=new FileInputStream("d:\\prg\\test\\1.txt");
int a=fileInput.read(buffer,0,256);
//这一行,为什么要把buffer转换成整型??
String str=new String(buffer,0,0,a);
//String函数的中参数0,0的意思是:把buffer的第零个字节转换吗?
System.out.println(str);