在Java中,可以使用以下方法来进行字符集转换:
使用String类的getBytes()方法将字符串转换为字节数组,再使用新的字符集构造一个新的字符串:String str = "Hello, 你好";byte[] bytes = str.getBytes("UTF-8");String newStr = new String(bytes, "GBK");System.out.println(newStr);使用InputStreamReader和OutputStreamWriter类来进行字符集转换: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);这些方法可以帮助实现在不同字符集之间的转换。


