关于byte数组转换的问题
byte 【】a =new byte 【1024】;
可以String s = new String(a);
这个可以用字符串来接收然后打印s将数组打印出来,
但是我看到一种读取方式,知道意思但是怎么想的,api上没有找到对应的转换方式
就是:
byte 【】a =new byte 【1024】;
int len = in.read(a);
String text = new String (buf,0,len);
看这个意思应该是说len是偏移量的吧,问题是在api上那里能找到??
谢谢各位大牛。
------解决方案--------------------Open Declaration java.lang.String.String(byte[] bytes, int offset, int length)
Constructs a new String by decoding the specified subarray of bytes using the platform's default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the subarray.
The behavior of this constructor when the given bytes are not valid in the default charset is unspecified. The java.nio.charset.CharsetDecoder class should be used when more control over the decoding process is required.
Parameters:
bytes The bytes to be decoded into characters
offset The index of the first byte to decode
length The number of bytes to decode
Throws:
IndexOutOfBoundsException - If the offset and the length arguments index characters outside the bounds of the bytes array
Since:
JDK1.1
这个eclipse里就能看
------解决方案--------------------0是偏移量,len是从偏移开始读取的长度
------解决方案--------------------2楼正解
从buf里面把0-len的字符串复制到text里面
------解决方案--------------------任何一个方法在源码里面都有的,找到看去
------解决方案--------------------这个是文件读取,byte 1024 是个缓冲区,in.read(1024) 一次读取1024个字节,然后将读取的字节转为字符串进行读取,你少写一步吧
------解决方案--------------------int len = in.read(a) 返回实际读取的字符数