java bytes[] 转 String
时间: 2023-12-15 11:31:19 浏览: 90
以下是两种Java byte[]转String的例子:
1.使用getBytes()和String构造函数
```java
String text = "你好哇aaaaaaaaa";
byte[] byteResult = text.getBytes("ISO-8859-1");
String textResult = new String(byteResult,"ISO-8859-1");
System.out.println(textResult); // 输出:你好哇aaaaaaaaa
```
2.使用Base64.encodeBase64String()和Base64.decodeBase64()方法
```java
import org.apache.commons.codec.binary.Base64;
import java.io.UnsupportedEncodingException;
public class ByteToString {
public static void main(String[] args) throws UnsupportedEncodingException {
String text = "你好哇aaaaaaaaa";
byte[] byteResult = text.getBytes("UTF-8");
String textResult = Base64.encodeBase64String(byteResult);
System.out.println(textResult); // 输出:5L2g5aW977yB5LiW5Lq677yB5YiX5L2gYWFhYWFhYWFhYQ==
byte[] baseResult = Base64.decodeBase64(textResult);
String result = new String(baseResult,"UTF-8");
System.out.println(result); // 输出:你好哇aaaaaaaaa
}
}
```
阅读全文