java如何转utf
转换字符串为UTF-8编码
在Java中,字符串默认使用UTF-16编码。若需转换为UTF-8编码的字节数组,可使用String类的getBytes方法并指定编码:
String originalString = "需要转换的字符串";
byte[] utf8Bytes = originalString.getBytes(StandardCharsets.UTF_8);
读取文件并转换为UTF-8
使用InputStreamReader和BufferedReader读取非UTF-8编码文件时,可指定输出编码为UTF-8:
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(new FileInputStream("input.txt"), "ISO-8859-1"))) {
String line;
while ((line = reader.readLine()) != null) {
byte[] utf8Bytes = line.getBytes(StandardCharsets.UTF_8);
// 处理UTF-8字节或重新转为字符串
}
}
写入UTF-8编码文件
通过OutputStreamWriter将内容以UTF-8编码写入文件:
try (Writer writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream("output.txt"), StandardCharsets.UTF_8))) {
writer.write("UTF-8编码的内容");
}
处理HTTP请求的UTF-8编码
在Servlet中设置请求和响应的UTF-8编码:
// 设置请求编码
request.setCharacterEncoding("UTF-8");
// 设置响应编码
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
转换其他编码至UTF-8
若源数据为其他编码(如GBK),需先按原编码解码再转为UTF-8:

byte[] gbkBytes = "字符串".getBytes("GBK");
String decodedString = new String(gbkBytes, "GBK");
byte[] utf8Bytes = decodedString.getBytes(StandardCharsets.UTF_8);






