温馨提示×

java如何输出中文字符

小亿
302
2023-08-31 17:56:44
栏目: 编程语言

在Java中,可以使用字符流和字节流来输出中文字符。

  1. 使用字符流输出中文字符:
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8"));
writer.write("你好,世界!");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
  1. 使用字节流输出中文字符:
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
OutputStream outputStream = new FileOutputStream("output.txt");
outputStream.write("你好,世界!".getBytes("UTF-8"));
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

请注意,为了正确地输出中文字符,需要指定正确的字符编码,例如UTF-8。

0