在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);
這些方法可以幫助實現在不同字符集之間的轉換。