我必须在Java中使用字节数组和字符串。我使用Java读取文件,并从getBytes()方法获取字节码,该方法为[B @ 1d1cdf7]
是否可以再次使用此代码工作。在我的程序中解码回java字符串。我需要字节数组,所以如何将这个值存储在字节数组中。我想要类似的程序,但我没有原始文本,而我只有字节数组,那么我该如何获取字符串结果。
您可以使用如下所示的内容:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
while(/* condition */ ) { // Get bytes from the file
// Append bytes to the byteArray
stream.write(bytes);
}
String str = new String(stream.toByteArray()); // Get the string representation
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句