在Java中,可以使用以下方法来进行字符集转换:
String str = "Hello, 你好";
byte[] bytes = str.getBytes("UTF-8");
String newStr = new String(bytes, "GBK");
System.out.println(newStr);
String str = "Hello, 你好";
byte[] bytes = str.getBytes("UTF-8");
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "GBK");
int c;
while ((c = reader.read()) != -1) {
writer.write(c);
}
writer.flush();
String newStr = new String(outputStream.toByteArray(), "GBK");
System.out.println(newStr);
这些方法可以帮助实现在不同字符集之间的转换。